Thursday, July 5, 2012

[Action Script] [AS3] Problem with checking for bullet collision July,2012

Hello
The following piece of code checks whether a bullet has collided with an enemy or a wall and acts accordingly, the problem is that if a bullet collides with an enemy and a wall it gives an error, this also happens when shooting in random directions and hitting walls and enemies.
After the error enemies are taken down with one shot, not two and if there is more than one enemy on stage the bullets just pass straight through them.

Here is the code:
Code: if (pistolBulletCount>0)
                                {
                                               
                                                for (var j:int = 0; j < zombieArmy.length; j++)
                                                {
                                                        enemy = zombieArmy[j];
                                                        // if the collsision occured remove the bullet and update health of enemy
                                                        for (var s:int = 0; s < pistolBullets.length; s++)
                                                        {
                                                                var blts:PistolBullet = pistolBullets[s];
                                                        }
                                                        if (enemy.hitTestObject(blts))
                                                        {
                                                                // if the enemy is dead, remove him as well
                                                                if (enemy.updateHealth(-50) <= 0)
                                                                {
                                                                        zombieArmy.splice(j, 1);
                                                                        enemy.parent.removeChild(enemy);
                                                                        zombienum--;
                                                                }
                                                                //RETURN; HERE TO AVOID ERROR
                                                        }
                                                }
                                       
                                       
                                                //Bullet Movement
                                                        for (var i:int = 0; i < pistolBullets.length; i++)
                                                        {
                                                                var pistolBlts:PistolBullet = pistolBullets[i];
                                                                pistolBlts.x-=xSpeed;
                                                                pistolBlts.y-=ySpeed;
                                                               
                                                                if (zombienum>0)
                                                                {
                                                                        if (pistolBlts.hitTestObject(enemy))
                                                                        {
                                                                                removeChild(pistolBlts);
                                                                                pistolBulletCount--;
                                                                        }
                                                                }
                                                               
                                                                if (pistolBlts.hitTestObject(wall))
                                                                        {                               
                                                                                if (PixelPerfectCollisionDetection.isColliding( pistolBlts, wall, this, true ) )
                                                                                {
                                                                                        removeChild(pistolBlts);
                                                                                        pistolBulletCount--;
                                                                                }
                                                                        }
                                                        }
                                }Here is the error:
Code: ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
        at flash.display::DisplayObjectContainer/removeChild()
        at MojaGra/Tick()
        at flash.utils::Timer/_timerDispatch()
        at flash.utils::Timer/tick()Any suggestions and help is appreciated as I checked and modified this code for quite a bit with no success.
[AS3] Problem with checking for bullet collision

Related Post



0 comments: