PDA

View Full Version : [AS2] Enemies occupying the same space


dmakinde
09-14-2011, 01:11 AM
Hey guys, working on my first major flash game and as you can expect, I'm running into some issues. It's my first time using arrays in this manner and I'm trying to get a script going that stops enemies from overlapping and also trying to get the enemy to properly splice the array when the enemy is defeated. My game is arranged like this:

For the sake of testing I have placed four enemies on the stage. Their instance names are Android1, 2, 3 and 4 respectively. I have set up the array in the actions layer like so:

levelOneEnemy = [_root.Android1, _root.Android2, _root.Android3, _root.Android4]
I've been able to get the androids to attack the player and react to the player attacks accordingly. (the actions are applied directly onto the movieclip)

I have two problems. When two enemies occupy the same mistakes the attacks become understandably erratic and hard to defend against because if there's some unfortunate timing, one enemy will attack while the other is ramping up, resulting in an onslaught of unavoidable attacks.

I tried to tackle that problem by introducing a variable that manages the enemies movement speed.

{
if(!occupying)
{
movementSpeed = 2;
}
else if (occupying)
{
this.stances.gotoAndStop("idle");
movementSpeed = 0;
}
and then attached the following code onto a controller movieclip

for (i = 0; i < _root.levelOneEnemy.length; i++)
{
var en1 = _root.levelOneEnemy[i];
for (j = i + 1; j < _root.levelOneEnemy.length; j++)
{
var en2 = _root.levelOneEnemy[j];
if (Math.abs(en1._x - en2._x) < 50)
{
//trace("occupying")
_root.levelOneEnemy[j].occupying = true;
}
else
{
//trace("not occupying")
_root.levelOneEnemy[j].occupying = false;
}
}
}
This kind of works, but presents a couple problems. First of all it doesn't stop all the enemies from overlapping. Some stop as they should but others seem to walk straight through.
The other problem is that if an enemy is defeated, sometimes the remaining enemies still can't go past the area where the first enemy died. I get the feeling that this is because the enemy is not correctly removed from the array. See when the enemy dies, this script executes.

if (dead)
{
this.stances.gotoAndStop("death");
}
within that frame, this executes when the animation completes

this._parent._parent.unloadMovie(this);
and the enemy is definitely removed visibly from the stage, but I get the feeling I didn't take the array into account, is it possible to unload a movie without removing it from the array?

I've attempted adding this

for (i = 0; i <= _root.levelOneEnemy.length; i++)
{
_root.levelOneEnemy.splice[1]
}
But it didn't seem to do anything. I'm going to go ahead and attribute that to my limited understanding of arrays.

rrh
09-14-2011, 05:47 PM
Definitely read more examples on using .splice() In that context, I think it would be something more like ".splice(i,1)"

Your main problem here:

if (Math.abs(en1._x - en2._x) < 50)
{
//trace("occupying")
_root.levelOneEnemy[j].occupying = true;
}
else
{
//trace("not occupying")
_root.levelOneEnemy[j].occupying = false;
}

If you loop through all possible values of i and j, the value of .occupying is going to be set by only the last time it's compared. Do you see what I mean?

It'll compare Android1 to Android3 and say they hit, right? So it sets Android3.occupying to true. But later it will compare Android2 to Android3 and if they don't hit, it will set Android3.occupying to false, and it'll forget about the collision between 1 and 3.

if (Math.abs(en1._x - en2._x) < 50)
{
//trace("occupying")
_root.levelOneEnemy[j].occupying = true;
}
else
{
//trace("not occupying")
}
So I would do it like this, and I would add another loop right before this loop that resets them all to false.