【问题标题】:AS3 Error 1009 when Debugging调试时出现 AS3 错误 1009
【发布时间】:2016-02-27 16:06:22
【问题描述】:

我正在尝试为我的太空飞船游戏创建一个游戏画面,当玩家的护盾达到 0 时,它会进入游戏画面并停止游戏。屏幕上的游戏正在运行,但我无法停止游戏。当玩家的护盾达到 0 时,我尝试将 Ship 设置为 null,但出现错误 1009。当“public function fGameStart(evt: Event): void { “执行,有没有办法可以在游戏结束时阻止此功能运行?非常感谢任何帮助!

public class Engine extends MovieClip {
    private var preloader: ThePreloader;

    public function Engine() {
        stage.addEventListener("gameSTART", fGameStart);
        stage.addEventListener("gameOVER", fGameOver);
    }

    private var numStars: int = 80;
    public static var enemyList: Array = new Array();
    private var ourShip: Ship;

    public function fGameStart(evt: Event): void {

        ourShip = new Ship(stage);
        ourShip.x = stage.stageWidth / 2;
        ourShip.y = stage.stageHeight / 2;
        ourShip.addEventListener("hit", shipHit, false, 0, true);
        stage.addChild(ourShip);

        for (var i: int = 0; i < numStars; i++) {
            stage.addChildAt(new Star(stage), stage.getChildIndex(ourShip));
        }

        addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

        function loop(e: Event): void {

            if (Math.floor(Math.random() * 20) == 5) {
                var enemy: Stinger = new Stinger(stage, ourShip);
                enemy.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
                enemy.addEventListener("killed", enemyKilled, false, 0, true);
                enemyList.push(enemy);
                stage.addChild(enemy);
            }

            else if (Math.floor(Math.random() * 80) == 5) {
                var enemy2: Stinger2 = new Stinger2(stage, ourShip);
                enemy2.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
                enemy2.addEventListener("killed", enemyKilled, false, 0, true);
                enemyList.push(enemy2);
                stage.addChild(enemy2);
            }
        }
    }

    public function fGameOver(e: Event) {
        gotoAndStop(4);
        ourShip = null;
    }


}

【问题讨论】:

    标签: actionscript-3


    【解决方案1】:

    ourShip 变量设置为null 毫无意义。它不会从stage 中删除DisplayObject,也不会从内存中删除它。实际上,您收到此错误的原因是您将其设置为 null

    您需要做的是停止触发loop 函数。

    public function fGameOver(e: Event) {
        gotoAndStop(4);
        //ourShip = null;
        stage.removeChild(ourShip);
        removeEventListener(Event.ENTER_FRAME, loop);
    }
    

    在这里为你的听众设置一个弱参考可能是个坏主意

    //addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
    //why not:
    addEventListener(Event.ENTER_FRAME, loop);
    

    【讨论】:

    • 如果函数“public function fGameStart(evt: Event):”包含其他游戏对象,我该如何删除整个函数?我试过 "removeEventListener("gameSTART", GameStart);"但它没有用。
    • 你不能删除函数,它们会在可能的时候被垃圾收集/flash感觉就像它
    • 如果您询问如何阻止侦听器函数触发,那么您就是这样做的,但是您缺少一个“f”
    猜你喜欢
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 2017-05-18
    相关资源
    最近更新 更多