Jump to content

Multiple event timers calling each other freeze the game


piotr
 Share

Recommended Posts

Hi all,

I want to show and hide a sprite with timers. With this code it seems that the timers are called multiple times every time and this causes the game to freeze and eventually crash.

Is there a way to add timers once and them just call them when needed?

Thanks

Enemy.Shaker = function (game,player) {
    game.time.events.add(1000, this.show, this);
};

Enemy.Shaker.prototype = Object.create(Phaser.Sprite.prototype);
Enemy.Shaker.prototype.constructor = Enemy.Shaker;

Enemy.Shaker.prototype.show = function() {
    this.visible = true;
    this.animations.play('isEnemy').onComplete.add(function() {
          this.game.camera.shake(0.05, 1000);
          game.time.events.add(1200, this.hide, this);
      }, this);
      console.log('show');  
};

Enemy.Shaker.prototype.hide = function() {
    this.visible = false;
    game.time.events.add(2000, this.show, this);
    console.log('hide');
};

 

Link to comment
Share on other sites

Thanks! I didn't know about onComplete.addOnce. Now it works.

Here's the full working code:

Enemy.Shaker = function (game,player) {
    game.time.events.add(1000, this.show, this);
};

Enemy.Shaker.prototype = Object.create(Phaser.Sprite.prototype);
Enemy.Shaker.prototype.constructor = Enemy.Shaker;

Enemy.Shaker.prototype.show = function() {
    this.visible = true;
    this.animations.play('isEnemy').onComplete.addOnce(function() {
          this.game.camera.shake(0.05, 1000);
          game.time.events.add(1200, this.hide, this);
      }, this);
      console.log('show');  
};

Enemy.Shaker.prototype.hide = function() {
    this.visible = false;
    game.time.events.add(2000, this.show, this);
    console.log('hide');
};

 

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...