Jump to content

Run function once, then after a delay


Göran
 Share

Recommended Posts

Hey,

 

I would like to call a function once, without a delay and after that, with a delay.

 

The function itself contains a for loop, that runs of a counter that I increment every 10 seconds.

Confusingly, the for loop doesn't recognize the incremented counter and still only runs the amount of times, the counter was originally at.

 

This would be my code so far:

  drawGobbla: function() {    this.timerTime = 10000;    this.gobblaGroup = this.game.add.group();    /* Add bitmap at given coordinates */    gobblaBitmap = this.game.add.bitmapData(20, 20);    /* Fill the bitmap with a color (rgba) */    gobblaBitmap.fill( 0, 250, 20, 1);    for(var i = 0; i < this.gobblaCount; i++) {      var gobbla = this.gobblaGroup.create(this.game.rnd.integerInRange(40, this.game.width - 60), 100, gobblaBitmap);      this.game.physics.enable(gobbla, Phaser.Physics.ARCADE);      gobbla.enableBody = true;      gobbla.body.velocity.y = this.game.rnd.integerInRange(200, 400);      gobbla.body.maxVelocity.y = 400;      gobbla.checkWorldBounds = true;      gobbla.events.onOutOfBounds.add(this.resetGobbla, this);      if(this.gobblaCount >= 30) {        this.gobblaCount = 0;      }    }  }

(gobblas are just squares really... :P)

 

In create: 

    this.gobblaCount = 1;    if(this.gobblaCount == 1) {      this.timerTime = 0;    }    this.timer = this.game.time.create(false);    this.timer.loop(this.timerTime, this.drawGobbla, this);    this.timer.start();

I am probably doing everything wrong. If so: Please tell me! I am just starting with all of this. Any help is greatly appreciated!

 

Basically what I want to achieve is: First one square falls from the top of the screen, then after 10 seconds 2 do, then 3 and so on.

Edited by Göran
Link to comment
Share on other sites

I don't see where you are incrementing this.gobblaCount at all...


Also, this is potentially not good:

for(var i = 0; i < this.gobblaCount; i++) {    ...    if(this.gobblaCount >= 30) {        this.gobblaCount = 0;    }}

If gobblaCount is 30, on the first run of the loop, it will reset to 0, and the loop will terminate, giving you only 1 gobbla.

Unless this is the intention, to restart the count every time 30 gobblas need to spawn.

In which case I'd put that if statement below the for loop so it is called once at the end, instead of every iteration.

Link to comment
Share on other sites

hi,

 

you can create something like this:
 

// decrease timeout after each tickvar time = 1000;var timer = game.time.create(false);var tick = function(){  time -= 50;  if(time > 0){     console.log("ticking - next tick in:", time);     timer.add(time, tick);  }  else{    console.log("destination reached");    timer.destroy();  }};timer.add(time, tick);timer.start();

p.s. you might find this useful: http://snook.ca/archives/javascript/javascript_pass

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...