Jump to content

An alternative to outOfBoundsKill


StrykerKKD
 Share

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.

 

Link to comment
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.

Link to comment
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 .

There is no performance downgrade compared to the 1.1.6 invaders version.

 

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 ?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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