对于每个循环不循环通过一切

时间:2014-07-09 21:55:40

标签: actionscript-3 flash for-loop

我有一个在onEnter函数中运行的循环(我知道不好)。我只希望它运行一次,但它不会遍历每个对象。我认为在下一帧运行之前没有时间。关于如何让它一直循环的任何想法?

private function onEnter(e: Event) {

        if (deleteThis == true) {
            if (timer == 0 || 1) {
                if (checkExplosion == false) {
                    gotoAndStop(2)
                    if (Main.bloonList.length > 0) {
                        for each(Main.bloon in Main.bloonList) {
                            if (Main.areaOfCollision(Main.bloon, this, 0)) {


                                if (Main.bloon.currentFrame != 5) {
                                    Main.money++;
                                    Main.bloon.nextFrame();
                                    Main.bloon.gotShot();
                                } else {
                                    Main.money++;
                                    Main.bloon.deleteBloon();
                                }

                            }

                        }
                    }
                    checkExplosion=true
                }
            }
        }
}

编辑 - deleteBloon();

public function deleteBloon()
    {
        this.parent.removeChild(this);
    }

1 个答案:

答案 0 :(得分:0)

首先,这个:

if (timer == 0 || 1)

应该是这样的:

if (timer == 0 || timer == 1)

然后您可能需要稍微更改一下代码。像这样的东西可以工作。我确实怀疑你的问题实际上可能在于deleteBloon函数。没有看到它我不能确定,但​​你的for循环没有(技术上)错误。

private function onEnter(e: Event) {
    if (deleteThis == true) {
        if (timer == 0 || timer == 1) {
            if (checkExplosion == false) {

                gotoAndStop(2);

                if (Main.bloonList.length > 0) {
                    // This isn't optimised as I wasn't sure what Main.bloon could be
                    for each(var bloon:Main.bloon in Main.bloonList) {

                        if (Main.areaOfCollision(bloon, this, 0)) {

                            if (bloon.parent && bloon.currentFrame != 5) {
                                bloon.nextFrame();
                                bloon.gotShot();
                            } /*else {
                               // Make sure that deleteBloon is deleteing the correct array index. This is likely where the issue is. 
                                bloon.deleteBloon();
                            }*/

                            Main.money++;

                        }

                    }

                }

                checkExplosion = true;
            }

        }

    }

}

这将是bloon:

import flash.utils.setTimeout;

function gotShot():void{
    // second parameter is time in milliseconds before executing
    setTimeout(deleteBloon, 250);
}
相关问题