【问题标题】:Parameter child must be non-null error in AS3AS3 中的参数 child 必须为非空错误
【发布时间】:2012-08-27 06:50:52
【问题描述】:

我有一个暂停游戏的代码,它运行暂停功能,如下所示:

public function onKeyPress(keyboardEvent:KeyboardEvent) :void
{
    //Check for pause
    if(keyboardEvent.keyCode == Keyboard.P)
    {
        //If the timer is still running
        if(gameTimer.running)
        {
            gameTimer.stop();
            Mouse.show();
            pauseText = new PauseText();
            pauseText.x = 150;
            pauseText.y = 100;
            addChild(pauseText);
            //If the player is using the mouse, resume by clicking on the player
            if(mouseControl)
            {
                player.addEventListener(MouseEvent.CLICK, resumeGame);
                pauseText.pauseInformation.text = "click on yourself";
            }
            else
            {
                pauseText.pauseInformation.text = "press 'p'";
            }
        }
        else
        {
            //Only allow the player to resume with P IF he is using the keyboard
            //This prevents cheating with the mouse.
            if(!mouseControl)
            {
                gameTimer.start();
                removeChild(pauseText);
                pauseText = null;
            }
        }
    }
}

游戏运行良好。在我第一次播放时,暂停功能起作用。但是,如果稍后我死了并重新启动游戏,然后暂停它,我会收到以下消息:

TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/removeChild()
    at Game/onKeyPress()

游戏仍然运行良好。但是,每次我暂停或取消暂停时,都会出现此错误。如果我再次死机,重新启动,然后暂停,会出现其中两个错误。据我所知,它似乎试图删除 pauseText ......但我在第一次播放时一直很好地删除它,我使用 removeChild() 然后将我的代码的其他部分设置为 null 并且它可以工作美好的。另外,如果我添加一个跟踪(“a”);在函数头之后的语句,我在输出面板上出现“a”之前得到错误。

怎么了?

补充说明: 如果我在第一次播放时根本不使用暂停功能,那么在第二次播放时调用它不会出错。

【问题讨论】:

    标签: actionscript-3 flash null flash-cs5


    【解决方案1】:

    将 removeChild 放入 'if' ,这将解决错误:

    if(pauseText.parent){
        pauseText.parent.removeChild(pauseText);
    }
    

    但无论如何你应该检查问题的根源是什么,也许'gameTimer.running'在开始时是假的?

    【讨论】:

    • 啊,解决了问题:) gameTimer.running 绝对不是假的,因为 gameTimer 运行整个游戏并且它的结果在屏幕上可见。然而,为了更好的衡量,我决定每次在暂停功能之前跟踪它。我在第一次尝试时得到了真实的陈述,但在第二次尝试每个“真”时,我也得到了“假”。然后我意识到这是因为我忘记删除 eventListener,因此在我的第二次游戏运行中,之前的监听器也在读取键盘输入并试图暂停。所以,我删除了 eventListener,现在它已经修复了。谢谢!
    【解决方案2】:

    您可能会实例化另一个 Game 对象(包含整个游戏的对象),而不会删除前一个游戏的事件侦听器。这可以解释这种行为,因为您有多个 KeyboardEvent.KEY_DOWN 侦听器处于活动状态,并请注意,当您停止游戏时,您很可能会停止其中的计时器,因此您的“if (gameTimer .running)" 语句被执行,但是计时器被有效地停止而没有生成 pauseText。所以,你错过了一个

    removeEventListener(KeyboardEvent.KEY_DOWN,onKeyPress);
    

    在您的游戏销毁代码中。

    【讨论】:

    • 是的……哈哈。我应该接受这个答案吗?考虑到首先是 turbosqel 帮助我解决了这个问题……
    • 你可能不会,我应该第一次仔细观察。 :)
    【解决方案3】:
    if (!mouseControl) {
        gameTimer.start();
        if (pauseText && contains(pauseText)) {
            removeChild(pauseText);
            pauseText = null;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-07-26
      • 2013-06-02
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 2018-06-01
      • 2013-07-28
      相关资源
      最近更新 更多