Jump to content

Killing the sprite and its action after a collision


CaptKiwi
 Share

Recommended Posts

Hi Guys,

 

A quick sum up of this question : The player bullet hits the enemy, the enemy sprite is killed, but the enemy shooting action continues to shoot rather then stop shooting. So you can imagine it like the sprite has been removed from on the screen, but its bullets continue to fire... very odd looking... 

 

More details :

 

Below is the code used to make the enemy shoot. The code loops through the total enemies on screen (i) and checks once the player is in range of each enemy, once in range the "enemyFire" function is run for that particular enemy (enemies.turret), which then shoots at the player. 
 
for (i=0; i < totalEnemies; i++)        {​if (this.game.physics.distanceBetween(enemies[i].turret, ship1) < rangeDistanceEnemy)        {                        if (game.time.now > nextFireEnemy && bulletsEnemy.countDead() > 0)            {               enemyFire();        }        }}
 
I am using the below function to destroy the sprite, it is called when the players bullet hits the enemy sprite :
function collisionBulletToEnemy(bullets, enemy){bullets.kill();enemy.kill();enemies[i].turret.kill();}
 
It works fine to remove the sprite, the question however, is how to stop the "enemyFire" function shown above from running once the particular sprite ( enemies.turret ) has been killed.
 
I guess something like the following is needed : Having a "var dead = false;" set for each enemy, then using "If (!dead) { enemyFire() }" but I'm not sure how to implement this for all enemies on screen.
 
I just can't wrap my head around it at the moment.
 
Cheers!
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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