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.
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.