0

我在计时器函数中有几个 if 语句,检查敌人的 hp 是否为 0。敌人被添加到数组中。当一个敌人的 hp 为 0 时,他们会从阵列中取出并移除。当数组长度为0时,游戏进入下一关。由于某种原因,当第一个敌人的生命值达到0时,它会自动进入下一个级别,甚至不需要检查其他敌人的生命值。它适用于第一级,我只有一个敌人,但第二级,我有两个敌人。我必须先打败第二个敌人,然后是第一个,才能更上一层楼。我认为这可能与第一个敌人是阵列中的第一个有关。关于如何更改代码以使其无视首先击败敌人的顺序的任何想法?

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

关卡定时器(设置关卡的定时器,运行一次):

    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();
   }

游戏计时器(检查敌人生命的计时器,保持运行):

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) {

    }
 }
4

1 回答 1

1

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.

于 2013-11-06T18:17:54.687 回答