Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Timed death for a specific instance of sprite


Recommended Posts



I am making a game with phaser and can't figure out a good way to do this. I want a comet to spawn offscreen, travel across the screen, then die when it gets to the other side.

I was thinking about doing a check when the comet is outside of the world, but they spawn outside of the world, so I don't think that would work correctly. Anyway, this is what I came up with so far, but when the timer runs out, it destroys every comet_left instead of the single instance of comet_left that has been alive for 5 seconds.

comet_left = comets.create(1000, y, "COMET_LEFT");comet_left.anchor.set(0.1, 0.5);comet_left.body.velocity.x = -600;comet_left.body.setSize(4, 4);game.time.events.add(5000, function() {       comet_left.destroy();});

Here is a link to my game if you wish to check it out for a better understanding - http://ruckbeard.koding.io/hellophaser/

To move the bubbles, you click and drag in whatever direction you want them to move, and to spawn the comet, you match 3 horizontally for right now.

Link to comment
Share on other sites

What I would do, is keep the outOfBounds check, but also add a flag at the start.


So, when you spawn the sprite you set a flag: this.wasVisible = false;

When the sprite enters the screen, you set the flag: this.wasVisible = true;


And in your update loop you could check something like:

if(sprite.outOfBounds) { if(sprite.wasVisible) sprite.destroy();} else  { sprite.wasVisible = true;}

So, you only destory the sprite if it is out of bounds and was previously on the screen.

Note that this is pseudocode, not exact phaser functions.

Link to comment
Share on other sites

Thank you for the response, but I found a better solution still using the timer functions.

game.time.events.add(3000, destroyComet, this, comet_left);

I found out how to pass the comet to the destroy function without referencing the "global" comet variable from inside of the comet creation function.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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