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?