Search code examples
arraysflashcollision

How to remove bullets on collision in a shooter game?


I'm trying to create a flash shooter game as my first project. But I can't remove the bullets and enemies when they are hit or off-screen. I've searched for a solution on the problem multiple times and copied about 4 of them (plus I've tried my own ideas) but they are not working. The current method of checking for collisions is:

    for each(var enemy:Enemy in basicEnemies)
    {
        for each(var projectile:Projectile in bullets)
        {
            if (projectile.x > enemy.x - enemy.width / 2 && 
            projectile.x < enemy.x + enemy.width / 2 &&
            projectile.y > enemy.y - enemy.height / 2 && 
            projectile.y < enemy.y + enemy.height / 2)
            {
                trace("collision!");
            enemy.enemyHealth = enemy.enemyHealth-5;
            projectile.projectileIsPassive = true;
            }
    }

My question is: How can I remove the enemies and bullets? removeChild and splice? I'd be grateful if this question got answered.


Solution

  • Yes, you would perform a removeChild and splice them off your container lists. removeChild(projectile);...