0
votes

My extremely short minigame has 4 objects on stage, after the game is done, I want them all to be removed from stage and for the game to reinitiate.

I've set it up like this (most bits taken out)

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

        }

This all works fine the first time around. Once it resets for the second time, i'm getting

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.

The parent continues to be the Stage, yet stage.removeChild is not the right syntax? I don't get it. Stackoverflow, would you please kindly point me in the right direction?

1
If you've removed code, you've possibly removed the problem. But in your code above you have a brackets issue. The mainGame function should have a closed bracket before the other other two functions begin. Also cupOne, CupTwo, CupThree seem to be local variables to mainGame - so would not be in scope in the other two functions. Hard to tell if you are saying you have removed code and we are not actually looking at what you compile.prototypical
The entire monstrosity would be too large for SO. cupOne,cupTwo and cupThree are indeed local variables of mainGame, but the removal of them need to be within a seperate function (as I don't want to addChild and then immediately removeChild. The resetGame function is a callback.) I should also note pretty much every aspect of the minigame is local to the mainGame function. The only things not inside that function is the startScreen (which calls mainGame in the firstplace) and the resetScreen (which is supposed to reset mainGame's state and call again (and is the root of the problem))techii
@prototypical here: pastebin.com/fQeCAiA9techii
You SHOULD NOT have resetGame and prepReset inside your mainGame function.prototypical
but if they are not inside the function they don't have access to any of the variables :S Please excuse me being a rookie here, but I'm not sure how to structure it in that case. edit: on second thought, I can move the variable declerations out of mainGame aswell, but addChild still needs to remain inside..techii

1 Answers

1
votes

Here's an example of how you might go about setting up this class :

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

I didn't declare your resetBtn or add event listeners etc, but that is the basic structure.

Putting the creation and adding of objects to the stage in a initGame() method, allows you to restart the game easily. The constructor only gets executed once, so it's best not to put things that you might want to do multiple times in the constructor.

Also, unless you want to create the cups each game reset, you could do this :

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

    }

This way, you'd not be creating new instances of those objects each time, but just adding/removing them from the display list as needed. Which might be what you desire.