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

        }

これはすべて、最初は正常に機能します。2回目にリセットすると、

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()メソッドに入れることで、ゲームを簡単に再開できます。コンストラクターは 1 回しか実行されないため、複数回実行したいことをコンストラクターに記述しないことをお勧めします。

また、ゲームのリセットごとにカップを作成したくない場合は、次のようにします。

    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 に答える