Bullet range (Phaser 3)

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(true, true);
    }
}

3 Likes