0

我的极短小游戏在舞台上有 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,请您指出正确的方向吗?

4

1 回答 1

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

    }

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

于 2013-04-24T23:32:03.143 回答