0

I'm making a game in AS3 for a school project, and I'm stuck on restarting the game. When the game is finished, there is a final screen, and then you can go back to the menu. This all goes well, but when I try and play again it says this:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/setChildIndex()
at MethodInfo-277()

I debugged this and I come to the next block of code:

               if (bringToFront==1){

                    setChildIndex(player,numChildren - 1);
                    setChildIndex(foppeLeft, numChildren - 1);
                    setChildIndex(foppeIdle, numChildren - 1);
                    setChildIndex(foppeRight, numChildren - 1);
                    setChildIndex(foppeDown, numChildren - 1);
                    setChildIndex(foppeUp, numChildren - 1);
                    setChildIndex(foppeTalent, numChildren - 1);
                    setChildIndex(abeIdle, numChildren - 1);
                    setChildIndex(abeUp, numChildren - 1);
                    setChildIndex(abeDown, numChildren - 1);
                    setChildIndex(abeLeft, numChildren - 1);
                    setChildIndex(abeRight, numChildren - 1);
                    setChildIndex(abeSkateIdle, numChildren -1);
                    setChildIndex(abeSkateUp, numChildren - 1);
                    setChildIndex(abeSkateDown, numChildren - 1);
                    setChildIndex(abeSkateLeft, numChildren - 1);
                    setChildIndex(abeSkateRight, numChildren - 1);
                    setChildIndex(abeSkateOn, numChildren-  1);
                    setChildIndex(abeSkateOff, numChildren - 1);
                    setChildIndex(marcoIdle, numChildren - 1);
                    setChildIndex(marcoDown, numChildren - 1);
                    setChildIndex(marcoUp, numChildren - 1);
                    setChildIndex(marcoLeft, numChildren - 1);
                    setChildIndex(marcoRight, numChildren - 1);
                    setChildIndex(marcoJumpUp, numChildren - 1);
                    setChildIndex(marcoJumpIdle, numChildren - 1);
                    setChildIndex(marcoJumpDown, numChildren - 1);
                    setChildIndex(marcoJumpRight, numChildren - 1);
                    setChildIndex(marcoJumpLeft,numChildren - 1);
                    setChildIndex(hudIngame,numChildren - 1);
                    setChildIndex(time,numChildren - 1);
                    setChildIndex(c1000PuP, numChildren - 1);
                    setChildIndex(univePuP, numChildren - 1);
                    setChildIndex(rabobankPuP, numChildren - 1);
                    setChildIndex(frieslandPuP, numChildren - 1);
                    setChildIndex(jakoPuP, numChildren - 1);

                    bringToFront=0;

                }

By deleting all this it is indeed not giving the error anymore, but in my game I built the levels from a tileSheet and an array. When I go to the next level, it won't load the tiles; it shows the first level, but code works for the second level.

(Please forgive my chaotic code; I'm new to this.)

The code is 2000+ lines, so I won't post that, but I will post some pieces that might clear it up a little.

When you reach the end, this happens:

        for (var mapEnd in mapsEndArr){
            if (player.hitTestObject(mapsEndArr[mapEnd])){
                if (oneRound==0){
                oneRound=1;
                player.x=0;
                }
            }
        }

        if (oneRound==1){
            //Making sure the arrows & pick up points get deleted when going to another map.

            if (foppeAction == true){

                for (var drawClueEnd in clueArray){
                    removeChild( clueArray[drawClueEnd] );
                }

                if (c1000PuPDrawn==true){
                    c1000PuP.visible=false;
                }

                if (univePuPDrawn==true){
                    univePuP.visible=false;
                }

                if (rabobankPuPDrawn==true){
                    rabobankPuP.visible=false;
                }

                if (frieslandPuPDrawn==true){
                    frieslandPuP.visible=false;
                }

                if (jakoPuPDrawn==true){
                    jakoPuP.visible=false;
                }
                foppeAction = false;
                freeze=0;

            }
            mapNumberY=0;
            mapNumberX=0;   

            mapsUpArr.splice(0, mapsUpArr.length);
            mapsDownArr.splice(0, mapsDownArr.length);
            mapsLeftArr.splice(0, mapsLeftArr.length);
            mapsRightArr.splice(0, mapsRightArr.length);
            wallArrayS.splice(0, wallArrayS.length);
            wallArray.splice(0, wallArray.length);
            iceArray.splice(0, iceArray.length);
            iceBorderArray.splice(0, iceBorderArray.length);
            clueArray.splice(0, clueArray.length);
            pickUpPoint.splice(0, pickUpPoint.length);

            c1000PuPDrawn=false;
            univePuPDrawn=false;
            rabobankPuPDrawn=false;
            frieslandPuPDrawn=false;
            jakoPuPDrawn=false;

            hudBars = new HudBars();
            hudBars.x = 0;
            hudBars.y = 0;
            addChild(hudBars);

            dispatchEvent( new NavigationEvent( NavigationEvent.FINAL ) );

            if (points==0){
                DocumentClass.endScreen.pointsText.text=("Je hebt geen logo's verzamelt.");
            }

            if (points>0 && points<5){
                DocumentClass.endScreen.pointsText.text=("Je hebt "+ points.toString() +"logo('s) verzamelt.");
            }

            if (points==5){
                DocumentClass.endScreen.pointsText.text=("Je hebt alle logo's verzamelt!");
            }

            points=0;

            //Removing all the childs and eventlisteners to clean stuff up in the cleanup function.
            cleanup();

            oneRound=2;

        }

and cleanup:

     function cleanup():void
        {
            removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
            removeEventListener(MouseEvent.MOUSE_DOWN, menuButtonClicked)
            removeEventListener(KeyboardEvent.KEY_DOWN, downKey);
            removeEventListener(KeyboardEvent.KEY_UP, upKey);
            removeEventListener(Event.ENTER_FRAME, loop);
            removeEventListener(Event.ENTER_FRAME, pauseLoop);

            for (var deleteChilds in deleteChildArr){
                removeChild(deleteChildArr[deleteChilds]);
            }

            setToNull();

        }

    }

    function setToNull():void
    {

    myGame=null;

    time=null;

    }

I puzzled around with it a bit, trying to get everything set to null, but I have the feeling somewhere it isn't happening. Any thoughts? This code is also set to null at some point, right here when you exit the final screen:

    public function finalScreen():void
    {
        endScreen = new EndScreen();
        endScreen.addEventListener( NavigationEvent.MENU, onRequestMenu );
        endScreen.x = 0;
        endScreen.y = 0;
        addChild(endScreen);

        playScreen=null;

    }

I hope any of this might lead to a valuable answer. I did some google search with different terms, and I found that my game somehow still thinks there are movieclips out there that need to be set to the front, but can't be reached because they're set to null?

Is it possible to clear everything and then start a new game just like you played the first time?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Tijmenh
  • 141
  • 1
  • 9
  • maybe set the oneRound var to 0 instead of null or try setting some of your other variables to 0 instead of null – nathan hayfield Jan 16 '13 at 00:36
  • You indicate in your large list that if you delete it it works. How about if you delete some of them? Can you zero in on the one thing it's breaking on? – Nathaniel Ford Jan 16 '13 at 00:56
  • Check if you don't call `removeChild` against any of the mentioned assets in the first quoted code block. Also, why setting child indexes if all you do is toggle visibility? And for cleanup of arrays, just set their length to 0, no splicing id needed. – Vesper Jan 16 '13 at 03:35
  • just array.length=0? wow, didn't know that. thank you. I checked carfully what I should remove and what not for the children. that's right like this. No, the real problem is the 'mapbuilder'. I got a tilesheet, and it rebuild a map everytime you move out of the screen where possible (mapX goes +1 or -1 for left and right and mapY+1 or -1 for up and down) so that works fine, but the problem is; it doesn't update it after game restart. And @NathanielFord, tried that. but it does it for every single setChildIndex in that list... also, some are static and some public. – Tijmenh Jan 16 '13 at 03:57
  • Oh, and I don't set any variables to null.. just some movieclips so the code won't work, and duplicate, anymore. – Tijmenh Jan 16 '13 at 03:58
  • So at first play, everything works fine, really fine; several bug testes and we had a ton of play testers also, no errors in there. but then, after you finish the game and you go back to the menu, and play again, trouble start. the first map loads fine; when I move out to the second map it shows the first map, but collision, pick up points and everything works for the second map.. I really wonder how this happen.. aside from the setChildIndexes not working.. – Tijmenh Jan 16 '13 at 04:01
  • from your sample code I've noticed a lot of removeChild and just one addChild - only for "endScreen" - if you have removed any child that later you want to use setChildIndex on then it will generate error as DisplayObject in subject is not a "child of a caller" anymore. – Lukasz 'Severiaan' Grela Jan 16 '13 at 07:44
  • After removing the childs, playScreen (so the movieclip containing this code) is set to null. Then you start a new playScreen, so a new playScreen(). Where all the childs are added again. – Tijmenh Jan 16 '13 at 13:44

1 Answers1

0

You have to do check that a child is in the parent now before deleting or changing index:

if (this.contains(player))    
    setChildIndex(player,numChildren - 1);
Serge Him
  • 936
  • 6
  • 8
  • Awesome, I guess that should work but I get the following error: TypeError: Error #1006: contains is not a function. at MethodInfo-283() would it be possible with: if (player!==null)? – Tijmenh Jan 16 '13 at 13:41
  • YOu can make check like: if (player && player.parent && player.contains(player)) – Serge Him Jan 16 '13 at 14:02