0
\$\begingroup\$

The bullet should be killed after it was shot from the player, and after a certain amount of time.

function create() { bullets = game.add.group(); bullets.enableBody = true; bullets.physicsBodyType = Phaser.Physics.ARCADE; game.physics.enable(bullets, Phaser.Physics.ARCADE); timer = game.time.create(false); for (var i = 0; i < 100; i++) { bullet = bullets.create(0, 0, 'bullet'); bullet.exists = false; bullet.visible = false; bullet.checkWorldBounds = true; bullet.events.onOutOfBounds.add(resetBullet, this); } } function update() { if (game.input.activePointer.isDown) { fire(); } } function fire() { if (game.time.now > bulletTime) { bullet = bullets.getFirstExists(false); if (bullet) { bullet.reset(player.x, player.y); game.physics.arcade.moveToPointer(bullet, 300); bulletTime = game.time.now + 150; timer.add(1000, resetBullet, this); timer.start(); } } } function resetBullet(bullet) { bullet.kill(); } 

As you can see I already tried adding a timer into the fire function which calls the resetBullet function after one second. But my game crashes one second after fire() was called:

Uncaught TypeError: Cannot read property 'kill' of undefined 

Does anyone know how to achieve the kill of the sprite?

\$\endgroup\$
1
  • \$\begingroup\$ I am not familiar with phaser but I know in javascript you should use yield will need to have delay.i think you can fix your problem by this link jessefreeman.com/game-dev/building-a-html5-game-with-phaser function shoot(){ if(nextShot>game.time.now){return;} // too early /* make shot */ nextShot = game.time.now + 1000; // wait at least 1 second (1000ms) to next shot } \$\endgroup\$ Commented Jul 8, 2016 at 11:03

2 Answers 2

2
\$\begingroup\$

I found out that objects can be killed with body.lifespan

Adding the line bullet.lifespan = 1000; in my fire() function fixed the problem

\$\endgroup\$
0
\$\begingroup\$

You can also call bullet.destroy() after a set amount of time as well.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.