AS3 hitTestObject for Array Works Sometimes and Other Times Doesn't?


#1

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.


#2

Bump… I’ve messed around with the script over the last couple of days but still no progress. Some of the “ghost” MC’s are adding to the array properly and being detected by the bullet MC’s, and about half of them aren’t, even though they use the exact same code… Any ideas?


#3

Figured it out… it was extremely easy and I’m surprised no one else was able to see it. All I had to do was type in this for the splicing:

ghosts.splice(ghosts.indexOf(ghost_template.ghost_hitBox),1);

So instead of doing the “i++” thing, I needed to get the actual specific index of where the MC was, not just… whatever it originally was splicing. I guess it was splicing the wrong or too many MC’s in the array at a time with the previous method…