In a Flash game I’m trying to make, you play as a character with a gun and you shoot your enemies (like most games). I have the enemies (“ghosts”) set in an array and currently they appear in random spots on the screen. Using hitTestObject, I have it so when you shoot, the bullets check if they are colliding with the array of enemies. About 3/4 times it’s able to detect it, and the rest of the time, it just doesn’t. I’m having a lot of trouble figuring it out.
Here is the bullet’s function:
function fireRevolver_r():void
{
var bullet:Bullet = new Bullet();
var a1 = mouseY - gun.y;
var b1 = mouseX - gun.x;
var radians1 = Math.atan2(a1,b1);
var degrees1 = radians1 / (Math.PI / 180);
var bulletTimer = 30;
var bulletTimer_end = “false”;
radians1 = Math.atan2(a1,b1);
degrees1 = radians1 / (Math.PI / 180);
addChildAt(bullet,under_hud);
bullet.x = gun.x + Math.cos(radians1) * 120;
bullet.y = gun.y + Math.sin(radians1) * 120;
bullet.rotation = degrees1;
bullet.addEventListener(Event.ENTER_FRAME, fire);
bullets.push(bullet);
function fire(event:Event):void
{
var thisBullet = event.currentTarget;
event.currentTarget.x += Math.cos(radians1) * 52.734375;
event.currentTarget.y += Math.sin(radians1) * 52.734375;
bulletTimer -= 1;
Bullet(bullet.currentTarget);
Bullet(event.currentTarget);
for (var i:Number = 0; i < ghosts.length; i++)
{
if (ghosts[i].hitTestObject(event.currentTarget))
{
if (bulletTimer_end == "false")
{
bulletTimer = 1;
bulletTimer_end = "true";
}
}
}
if (bulletTimer<=0)
{
for (var u:Number = 0; u < bullets.length; u++)
{
bullets.splice(u,1);
}
removeChild(thisBullet);
thisBullet.removeEventListener(Event.ENTER_FRAME, fire);
bulletTimer = 0;
}
}
}
It’s weird because I even used trace for the hitTestObject portion of the script, and if the bullet hit and the child got removed, it would in fact notify me. Other times, however, the bullet would just continue to fly through the enemies and not seem to understand that it just collided with them.
Here is a portion of the enemies’ script:
function spawn_ghost():void
{
ghost_template = new Ghost_Template ;
addChildAt(ghost_template,under_hud);
ghosts.push(ghost_template.ghost_hitBox);
ghost_template.y = Math.floor(Math.random() * wall_bottom01.y) + wall_top01.y;
ghost_template.x = Math.floor(Math.random() * wall_right01.x) + wall_left01.x + 1280;
ghost_template.addEventListener(Event.ENTER_FRAME,ghost_functions);
function ghost_functions(event:Event):void
{
Ghost_Template(ghost_template.currentTarget);
Ghost_Template(event.currentTarget);
//Bullet Interaction
var thisGhost = event.currentTarget;
for (var i:Number = 0; i < bullets.length; i++)
{
if (bullets[i].hitTestObject(event.currentTarget.ghost_hitBox))
{
HP -= player_gunDamage;
}
}
//Death
if (HP<=0)
{
for (var u:Number = 0; u < ghosts.length; u++)
{
ghosts.splice(u,1);
}
removeChild(thisGhost);
thisGhost.removeEventListener(Event.ENTER_FRAME, ghost_functions);
}
}
}
It seems to be completely random… I’ll spawn about 5 ghosts and usually 1 or 2 of them doesn’t seem to be noticed by the bullets (though the ghosts always know when they are getting hit). Any ideas? Thanks.