Jump to content

Timer event slows down game


goowik
 Share

Recommended Posts

Hi,

 

I'm trying to recreate Bomberman as a fun project when I have nothing to do at work.

 

As for now I'm testing out the creation and destroying of bombs using a timer event. See code:

update: function () {        this.game.physics.arcade.collide(this.player, this.walls);                if (this.cursor.left.isDown) {            this.player.body.velocity.x = -150;        }        else if (this.cursor.right.isDown) {           this.player.body.velocity.x = 150;        }         else {           // Stop the player           this.player.body.velocity.x = 0;        }        if (this.cursor.up.isDown) {           this.player.body.velocity.y = -150;        }        else if (this.cursor.down.isDown) {           this.player.body.velocity.y = 150;        }         else {           // Stop the player           this.player.body.velocity.y = 0;        }                this.space.onDown.add(function() {            var bomb = this.game.add.sprite(this.player.x, this.player.y, 'bomb');            this.game.time.events.add(Phaser.Timer.SECOND * 3, function(){                bomb.kill();            }, this);        }, this);    },

So as a test I try to place a bomb using the "space" key and destroy it after 3 seconds. Now as for the first few bombs no lag is showing.
After about 20 bombs being destoryed and placing one, the player stays for about half a second and then moves on.

 

 

I'm no expert in threads nor events, but I wonder if the created events keep running in the background (even after it killed the bomb object).

 

How come?

Link to comment
Share on other sites

Hi, I did not run your code but it seems to me that you are adding the event in the wrong place.

 

The update method is executed ~60 times per second, so you are adding a huge amount of events and it forces the garbage collector to run.

 

I suggest to you yo add the event in the create() or init() method.

 

Other option is to check if the space hey is down, but not adding an event, else like you did with the cursor.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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