How to kill bullet after a certain amount of time, or certain amount of distance ? I'm using JavaScript and Phaser 3.
if (keyA.isDown && time > lastFired || isDown && time > lastFired) {
var bullet = bullets.create(player.x , player.y, 'bullet');
bullet.setVelocity( -800, 0);
lastFired = time + 90;
}
If it should be after some time, you could do something like this. (here some more details from the Docs: from the Docs )
But to be honest I don't know, if this would impact the performance, or not.
...
// the 500 ist the timeout in ms
this.time.delayedCall(500, () => bullet.destroy();
....
Or you cout use a time event
this.time.addEvent({
delay: 500,
callback: ()=>{
bullet.destroy()
},
loop: false
});
for destroying by distance: depending if it is one bullet or many. you could add the bullet to a group and check the distance in the update function, and it its too far you detroy it/them.