Okay, here is the current progress. Click to shoot enemies, rupees, and hearts.
Alright, so far I am able to shoot enemies and other objects. Then I was able to (in my own, probably inefficient, way) cause items to spawn from defeated enemies. In this case, when one of the monsters dies, a heart will drop out of it.
Here is my current collision code that allows this:
private function checkCollisions():void
{
var bullet:MovieClip;
var rupee = new Rupee();
var impact = new Impact();
var heart = new ZeldaHeart();
for(var h:int = 0; h < heartArray.length; h++)
{
heart = heartArray[h];
for (var k:int = 0; k < enemyArray.length; k++)
{
enemy = enemyArray[k];
for (var j:int = 0; j < rupeeArray.length; j++)
{
rupee = rupeeArray[j];
for (var i:int = 0; i < bulletArray.length; i++)
{
bullet = bulletArray[i];
if (rupee.hitTestPoint(bullet.x,bullet.y,true))
{
stage.removeChild(bullet);
stage.addChild(impact);
impact.x = bullet.x;
impact.y = bullet.y;
rupee.rupeeHealth--;
bulletArray.splice(i,1);
//rupeeArray.splice(i,1); //problem line
if (rupee.rupeeHealth <= 0)
{
stage.removeChild(rupee);
}
}
if (enemy.hitTestPoint(bullet.x,bullet.y,true))
{
stage.removeChild(bullet);
stage.addChild(impact);
impact.x = bullet.x;
impact.y = bullet.y;
enemy.enemyHealth--;
enemy.gotoAndPlay("enemyHit");
bulletArray.splice(i,1);
if (enemy.enemyHealth <= 0)
{
heart = new ZeldaHeart();
rupee = new Rupee();
stage.addChild(impact);
stage.addChild(heart);
heart.x = enemy.x;
heart.y = enemy.y;
heartArray.push(heart);
impact.x = enemy.x;
impact.y = enemy.y;
impact.width = 60;
impact.height = 60;
stage.removeChild(enemy);
//enemyArray.splice(k,1); //problem line
}
}
if (heart.hitTestPoint(bullet.x,bullet.y,true))
{
stage.removeChild(bullet);
stage.addChild(impact);
impact.x = bullet.x;
impact.y = bullet.y;
bulletArray.splice(i,1);
stage.removeChild(heart);
//heartArray.splice(h,1); //problem line
}
}
}
}
}
}
So, I have each shoot-able item in its own array being tested against the bullet array. Not sure if the way I have it set up is the best way, but it's mostly working correctly.
Some issues I've had: Before, when I was trying to make the collisions with my "heart" items, they wouldn't react to the bullets. It wasn't until I changed the initial heartArray declaration at the top of my program from this:
public var heartArray:Array = [];
into this:
public var heartArray:Array = [heart];
so, I added an initial value of "heart" to the array.
I guess because the array had no value in it at the start of the program, the entire loop was affected and the hearts were unreadable even if they were added to an array later. And I'm not that naive. I know that even though this solution "worked", it is probably a terrible way of doing it.
Now, about the splice. I've tried it in all sorts of different ways, but to no avail. I am no longer getting errors when I try to splice, but the collision checks stop working when I begin to splice objects from their arrays.
For instance, the situation when I only had the splice on my hearts and on my enemies: My hearts would disappear when shot as long as there was still an enemy on the screen. As soon as the final enemy was destroyed and the enemy array was spliced empty, I could no longer interact with any remaining hearts.
I know there must be some way to splice from my arrays and still keep the collision test working. Maybe by separating the collision tests? I thought combining them would be better, but if they are all so dependent on each other, maybe it's not the best idea.
Thoughts?