문제

I have a couple of if statements within a timer function, checking if the enemy's hp is 0. The enemies are added into an array. When one enemy hp hits 0, they are taken from the array and removed. When the array length is 0, the game goes to the next level. For some reason, when the first enemy's hp reaches 0, then it automatically goes to the next level without even checking the other enemy's hp. It worked on the first level, where I only had 1 enemy, but the second level, I have two enemies. I would have to defeat the second enemy, then the first in order for it to go to the next level. I think it may have something to do with the first enemy being the first in the array. Any ideas on how I can change the code to make it disregard the order in which enemy is defeated first?

public var enemy:Enemy = new Enemy();
public var enemy2:Enemy = new Enemy();
public var enemyArray:Array = [];

Level Timer(Timer that sets up the level, runs once):

    if (level.levelNumber == 1)
    {
            enemyArray.push(enemy);
            addChild(player);
            addChild(enemy)

            enemy.enemyMoveTimer.start();
            enemy.enemyAttackTimer.start();
            onGameTimer.start();
    }
    if (level.levelNumber == 2)
    {

        enemyArray.push(enemy, enemy2);
        addChild(player);

        addChild(enemy)
        addChild(enemy2)

        enemy.enemyMoveTimer.start();
        enemy.enemyAttackTimer.start();

        enemy2.enemyMoveTimer.start();
        enemy2.enemyAttackTimer.start();
        onGameTimer.start();
   }

Game timer(timer that checks the enemy lives, keeps running):

if (enemyArray.length == 0)
{
    trace("NEXT LEVEL");
    removeChild(player);//remove player from the current level
    onLevelTimer.removeEventListener(TimerEvent.TIMER, timerLevel);
    levelNumber += 1;

    onLevelTimer.addEventListener(TimerEvent.TIMER, timerLevel);


    onGameTimer.stop();
}
if (enemy.enemyHP <= 0)
{
    enemy.enemyHP = 0;
    enemy.enemyMoveTimer.stop();
    enemy.enemyAttackTimer.stop();
    trace("DEAD");
    enemyArray.splice(0,1);
    try
    {
           removeChild(enemy)
    }
    catch (e:ArgumentError){

    }
}
if (enemy2.enemyHP <= 0)
{
    enemy2.enemyHP = 0;
    enemy2.enemyMoveTimer.stop();
    enemy2.enemyAttackTimer.stop();
    trace("DEAD");
    enemyArray.splice(1,1);
    try
    {
        removeChild(enemy2)
    }
    catch (e:ArgumentError) {

    }
 }
도움이 되었습니까?

해결책

There's more code there that could be the cause of the issue, so I'm just going to assume that it looks something like what I've written below. It also dispenses with multiple if statements, in favor of simply iterating over your enemyArray, checking each as you wanted to (behold the power of loops!).

var enemyMoveTimer:Timer = new Timer(1000, 0);
enemyMoveTimer.addEventListener(TimerEvent.TIMER, enemyTimerListener);
enemyMoveTimer.start();

var enemyArray:Array = [new Enemy("enemy1"), new Enemy("enemy2")];

function enemyTimerListener(e:Event):void {
    for (var i:String in enemyArray) {
        var enemy:Enemy = enemyArray[int(i)];

        if (enemy.enemyHP <= 0) {
            enemy.enemyMoveTimer.stop();
            enemy.enemyAttackTimer.stop();
            trace("DEAD");
            enemyArray.splice(int(i),1);
            if (enemy.parent != null) {
                removeChild(enemy);
            }
        }
    }
}

What isn't shown is how you're detecting and advancing to "the next level". How are these enemies being added to the array? Since it seems you've rolled your own Enemy class, what's that class look like? Why are you using try ... catch?

In any case, if the above works, fun; otherwise, we'll need more code to know what's going on.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top