I’m creating a game with AS3 in Flash Professional CS5.5.
In this game I have an “again” button, so that the player can reset the level and start from new.
My problem now is:
After the clicked “again” the stage becomes null.
All I do in the “ResetLevel” method is, that I set the x and y positions of some elements back to 0, remove some items from the movieclip, but I don’t remove ALL items from the display list. So the background, the hud, the plane isn’t removed from the movieclip. Here a sketch of my displaylist. The removeable items are sometimes zero, sometimes they are 30 or more items (depends on playtime, and so on)
Displaylist:
stage
|-- Game movieclip
|--LevelBackground
|--Removeable item
|--Removeable item
|--Removeable item
|--Plane
|--HUD
But after removing the “removeable items” and setting the position coordinates of levelbackground and plane the stage is null.
Maybe someone can help me to point me to a solution for this problem.
EDIT:
The “ResetLevel”-method will be called inside the “game movieclip” and the stage will be accessed from the “game movieclip”, too. So I don’t remove the “game movieclip” from the displaylist when I reset the level. I only remove some elements, that the game movieclip contains from the movieclip.
Here some pseudocode from the “game movieclip class” (GameMC):
public class GameMC extends Sprite {
//Some properties here
public function GameMC() {
//Some code here
//--Events--
this.addEventListener(Event.ADDED_TO_STAGE, Init, false, 0, true);
this.addEventListener(Event.REMOVED_FROM_STAGE, Removed, false, 0, true);
}
private function Init(e:Event) {
this.removeEventListener(Event.ADDED_TO_STAGE, Init);
//Some Code here
}
private function ResetLevel() {
//Some Code here, too
if(removeItemArray.length > 0) {
for(i = 0; i < removeItemArray.length; i++) {
currentRemoveableItem = removeItemArray[i];
this.removeChild(currentRemoveableItem );
removeItemArray.splice(i, 1);
}
}
level.x = 0;
level.y = 0;
trace(stage); //Will output null
}
}
Now I used to store the stage into a property and access this: