An alternative to outOfBoundsKill

Recommended Posts

I'm making a Space invader clone(based on the Phaser example) and i read in the migration guide about outOfBoundsKill being an expensive call, therefore we should use something else.

Aha, but what should we use?

In this kind of situation, i went to the Phaser docs:http://docs.phaser.io/ and i started searching for something that i can use to solve this problem.

In the sprite class i found the lifespan variable. http://docs.phaser.io/Phaser.Sprite.html#lifespan

"The lifespan of the Sprite (in ms) before it will be killed."

Cool, but how should i calculate the time(lifespan)?

Time = Distance / Velocity

Time will have to be in ms.

The distance will be in pixels.

The Velocity has to be in pixels/ms, but in Phaser Velocity's value is in pixels/s , so it need to be converted.

We know that 1s = 1000ms(1/1000s = 1ms), so we need to divide Velocity by 1000.

Finally we have:

Time = Distance / (Velocity/1000)

In my case:

`_bullet.lifespan = _game.height / (_bulletSpeed/1000);`

Use it with caution, because I don't really know if using lifespan gives us a better performance.

Maybe Rich can enlighten us or i test it out.

Share on other sites

There are a number of ways that would be faster than getBounds, but all depend on a simple display list. So if you've got moving groups or heavily nested children this won't work, but otherwise:

1) If you've got a physics body, then do a rectangle intersect check with the world vs. the body. If it fails it's out of bounds.

2) If you're doing a game where maybe the sprites just move down the screen, then just check their y coordinate each update, and if > game height, kill them.

Really depends what type of game you're making as to the fastest approach. Also when I say that getBounds is expensive, it is, but only if you've thousands of objects. If you've a relatively small amount it won't make any difference. So maybe start with it and only change if you see slow down.

Share on other sites

@StryderKKD

For this very example (invaders), porting from 1.1.6 to 2.0 I just added checkWorldBounds = true to every bullet sprite before the former outOfBoundsKill = true .

Here's my clone (WIP, in french) : http://raphael.bousquie.fr/jeux/game2/

It's for my son : he loves have his pet toys battling together on his bedroom floor.

So in this dedicated video game, he must throw tennis balls to his pet toys. Pet toys throw him dirty socks (there's a secret message in this allegory for him to remember to keep his bedroom clean, arf). The backgroung is his bedroom floor.

High customization, isn't it ?

Share on other sites

2) If you're doing a game where maybe the sprites just move down the screen, then just check their y coordinate each update, and if > game height, kill them.

This is precisely what I've been doing lately and I'm always well within the frame budget.