samme
September 26, 2020, 2:37am
1
A bullet pool is a sprite pool where all the sprites are initially inactive. They are activated when fired and then deactivated when they collide or expire.
Bullet pools
Bullet pools with extended game object class
More examples
Since Phaser v3.50.0 the WORLD_STEP event passes the Arcade Physics delta time. You can use it to implement precise bullet lifespans. Bullet lifespans are slightly simpler than bullet ranges since no distances need to be calculated.
function fireBullet (bullet)
{
// …
bullet.setState(BULLET_LIFESPAN);
}
function updateBullet(bullet, delta) {
bullet.state -= delta;
if (bullet.state <= 0) {
bullet.disableBody(true, true);
}
}
Here’s a way to make bullets expire at a certain distance traveled, taking advantage of newVelocity .
It’s a little more efficient to make bullets expire after a physics-time limit is reached instead. Phaser v3.50.0 will make that easier.
function fireBullet (bullet, x, y, vx, vy)
{
// …
// Set the range in pixels
bullet.state = 300;
}
function updateBullet (bullet)
{
bullet.state -= bullet.body.newVelocity.length();
if (bullet.state <= 0)
{
bullet.disableBody(…
4 Likes