ArgumentError:游戏“重置”后错误#2025,可能是范围错误

时间:2013-04-24 22:12:55

标签: actionscript-3 flash

我极短的迷你游戏在舞台上有4个物品,游戏完成后,我希望所有这些物品都从舞台上移除并让游戏重新开始。

我已经这样设置了(大多数比特都取出来)

function mainGame():void {
            var theCoin:coin = new coin();
            var cupOne:cup = new cup();
            var cupTwo:cup = new cup();
            var cupThree:cup = new cup();
            stage.addChild(theCoin);
            trace(theCoin.parent);
            stage.addChild(cupOne);
            trace(cupOne.parent);
            stage.addChild(cupTwo);
            trace(cupTwo.parent);
            stage.addChild(cupThree);
            trace(cupThree.parent);


            function prepReset():void 
            {
                cupOne.removeEventListener(MouseEvent.CLICK, liftCup1);
                cupTwo.removeEventListener(MouseEvent.CLICK, liftCup2);
                cupThree.removeEventListener(MouseEvent.CLICK, liftCup3);
                stage.addChild(resetBtn);
                resetBtn.addEventListener(MouseEvent.CLICK, resetGame);

            }

            function resetGame(event:MouseEvent):void 
            {  
                stage.removeChild(cupOne);
                stage.removeChild(cupTwo);
                                    stage.removeChild(cupThree);
                letsgoagain();
            }

        } // end mainGame

        function letsgoagain():void
        {
            stage.removeChild(resetBtn);
            mainGame();
            trace("RESET")

        }

第一次这一切都很好。一旦它第二次重置,我就得到了

Game Started
[object Stage]
[object Stage]
[object Stage]
[object Stage]
RESET
[object Stage]
[object Stage]
[object Stage]
[object Stage]
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display::DisplayObjectContainer/removeChild()
    at Function/coinGame/$construct/mainGame/resetGame()[*\coinGame.as:147]
Cannot display source code at this location.

父级仍然是舞台,但stage.removeChild不是正确的语法?我不明白。 Stackoverflow,请你指点我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

以下是您可以如何设置此课程的示例:

public class coinGame extends MovieClip
{

    public var theCoin:coin;
    public var cupOne:cup;
    public var cupTwo:cup;
    public var cupThree:cup;

    public function coinGame():void 
    {
        initGame();
    }

    public function initGame():void
    {
        theCoin = new coin();
        cupOne = new cup();
        cupTwo = new cup();
        cupThree = new cup();

        addChild(theCoin);
        addChild(cupOne);
        addChild(cupTwo);
        addChild(cupThree);

    }

    public function prepReset():void 
    {
        cupOne.removeEventListener(MouseEvent.CLICK, liftCup1);
        cupTwo.removeEventListener(MouseEvent.CLICK, liftCup2);
        cupThree.removeEventListener(MouseEvent.CLICK, liftCup3);
        addChild(resetBtn);
        resetBtn.addEventListener(MouseEvent.CLICK, resetGame);

    }

    public function resetGame(event:MouseEvent):void 
    {  
        removeChild(cupOne);
        removeChild(cupTwo);
        removeChild(cupThree);
        letsgoagain();
    }

    function letsgoagain():void
    {
        removeChild(resetBtn);
        initGame();
    }
}

我没有声明你的resetBtn或添加事件监听器等,但这是基本结构。

使用initGame()方法创建和添加对象到舞台,可以让您轻松重启游戏。构造函数只执行一次,因此最好不要在构造函数中多次放置您想要执行的操作。

此外,除非你想重置游戏,否则你可以这样做:

    public function coinGame():void 
    {
        theCoin = new coin();
        cupOne = new cup();
        cupTwo = new cup();
        cupThree = new cup();
        initGame();
    }

    public function initGame():void
    {

        addChild(theCoin);
        addChild(cupOne);
        addChild(cupTwo);
        addChild(cupThree);

    }

这样,您不会每次都创建这些对象的新实例,而只是根据需要在显示列表中添加/删除它们。这可能是你想要的。