Restarting levels

Hi,

I am fairly new to box2d. My main objective is to garbage collect all of the bodies and sprites within the level once game over occurs. After this happens, I want the sprites and bodies to be drawn once again on screen. I have tried multiple ways, as suggested online, but nothing has worked so far.

What I am currently doing is running the main level within a movieclip and playing it through my main class. I do this by adding a child of this movieclip. When game over occurs, I summon the gameover screen and try to remove the child of the movieclip. When I do this, a long error comes my way. I am not sure why this is occuring because I am able to add the child of the main play level, but I am not able to remove the child in the same class. This is what I am doing:

Code:
public var playGame:MainGame;
public var gameOverScreen:GameOverScreen;

public function Main()
{
     playGame = new MainGame();
     playGame.addEventListener(Event.GAME_OVER, gameOver(); //Checking to see whether or not gameover has occured
     addChild(playGame); //Adds child correctly
}

public function gameOver(e:Event):void {
   gameOverScreen = new GameOverScreen();
   gameOverScreen.addEventListener(Event.HIT_RESTART, onRestart); //Checks to see whether or not I click restart on the GameOverScreen movieClip
   trace("GameOver"); //GameOver traces correctly
   addChild(gameOverScreen); //The child get added correctly
         
        removeChild(playGame); //Gives me many errors
   playGame = null; //No errors. I've tried without this, makes no difference
   trace(Vals.world.GetBodyCount()); //traces more than 0 bodies
}

private function onRestart(e:Event):void
{
   playGame = new MainGame();
   addChild(playGame);
   trace("game restarted"); // Traces correctly
         
   removeChild(gameOverScreen); //Does not give me errors, but the gameOverScreen does not disappear
   gameOverScreen = null;
         
}



I also tried creating and activating a destroyLevel() function containing the code below, yet when I print the body count, it remains at more than 0. With this method, I am not sure how to also remove the sprites.

Code:
var node:b2Body = m_world.GetBodyList();

while (node)
    {
        var b:b2Body = node;
        node = node.GetNext();
        m_world.DestroyBody(b);
    }


To worsen the case, with either of these methods (even both together), when I click restart the world speeds up 2x every time I click restart.

I am using box2d 2.02 in flash.

Any help would be appreciated.
Thank you.
Anyone have a solution?
There's a lot of stuff in there that I don't know, like how you're initializing your world etc. etc. But I've found the best method for level interchange to be when you make the objects for your level, push them into an array of b2 Bodies, and then when you want to get rid of them all - just iterate through that array and destroy them one by one, like so:
Code:
      public function gameOver():void {
         for each (var i:b2Body in LevelArray)
         {
            b2World.DestroyBody(i);
         }
      }


Let me know how that turns out!
Schrodinger'sCat wrote:
There's a lot of stuff in there that I don't know, like how you're initializing your world etc. etc. But I've found the best method for level interchange to be when you make the objects for your level, push them into an array of b2 Bodies, and then when you want to get rid of them all - just iterate through that array and destroy them one by one, like so:
Code:
      public function gameOver():void {
         for each (var i:b2Body in LevelArray)
         {
            b2World.DestroyBody(i);
         }
      }


Let me know how that turns out!


Thanks a lot for the reply, and I'm glad to say it worked!

I have another problem, despite this. My next objective is to create multiple levels for my game, but the problem is, I'm not sure how I should.

My idea was to store every individual level in a movieclip, and adding them as a child whenever they should be brought up. If I do this, though, I wouldn't know how to set up preloaders for every individual level, since I am unaware of a method to preload movieclips, but only swfs.

Any ideas?
I suppose it depends on how you're planning on coding your game, whether it's wholly programmatically or with the support of a visual editing system like Flash Professional or the like, sounds like you're aiming for the latter. The easiest way to go about it if you were to do things THAT way would be to have a read over the kerp.com tutorials on actors and costumes, they give a really good insight into the most effective way of programming out levels that you can chop and change. To give you an idea of what it looks like in the back end, here's a snippet of code from a game I made a while ago:

Code:
var _level1:Sprite = new Level1();

private function drawLevel():void
      {
                  
         if (_level == 1) {
                                this.addChild(Level1);
            var level1WallShapes:Array = [[new Point(0, 0), new Point(50, 0), new Point(50, 500), new Point(0, 500)]];
            var level1FloorShapes:Array = [[new Point(0, 0), new Point(600, 0), new Point(600, 50), new Point(0, 50)]];            
            var level1LeftWall:ArbiStaticActor = new ArbiStaticActor(this, new Point(99.4, 18), level1WallShapes, null);
            _allActors.push(level1LeftWall);            
            var level1RightWall:ArbiStaticActor = new ArbiStaticActor(this, new Point(651.9, 18), level1WallShapes, null);
            _allActors.push(level1RightWall);            
            var level1Floor:ArbiStaticActor = new ArbiStaticActor(this, new Point(104, 502), level1FloorShapes, null);         
            _allActors.push(level1Floor);
            
         } else if (_level == 2) { etc etc etc.


That's probably pretty hard to read through without looking at the kerp.com tutorials, but essentially, I made the static graphics for the level in professional and then them as a class (Level1), and then in my draw level function which is invoked at various points, the code searches for an int variable which is increased and decreased as the player progresses through the levels of the game. The if function basically gets the level integer and then draws out the walls of the level ontop of the graphic which is added to the stage when the draw level function is invoked.

I dunno, it was a long time ago that I did this code, looking back at it now kinda makes me feel like there is a better way of doing things, I would watch a few of kerps tutorials at http://www.kerp.net/box2d/ on actors and the actor classes that he creates as they are a real helper in understanding how box2d deals with all of that sort of stuff.

Let me know how you go!
Schrodinger'sCat wrote:
I suppose it depends on how you're planning on coding your game, whether it's wholly programmatically or with the support of a visual editing system like Flash Professional or the like, sounds like you're aiming for the latter. The easiest way to go about it if you were to do things THAT way would be to have a read over the kerp.com tutorials on actors and costumes, they give a really good insight into the most effective way of programming out levels that you can chop and change. To give you an idea of what it looks like in the back end, here's a snippet of code from a game I made a while ago:

Code:
var _level1:Sprite = new Level1();

private function drawLevel():void
      {
                  
         if (_level == 1) {
                                this.addChild(Level1);
            var level1WallShapes:Array = [[new Point(0, 0), new Point(50, 0), new Point(50, 500), new Point(0, 500)]];
            var level1FloorShapes:Array = [[new Point(0, 0), new Point(600, 0), new Point(600, 50), new Point(0, 50)]];            
            var level1LeftWall:ArbiStaticActor = new ArbiStaticActor(this, new Point(99.4, 18), level1WallShapes, null);
            _allActors.push(level1LeftWall);            
            var level1RightWall:ArbiStaticActor = new ArbiStaticActor(this, new Point(651.9, 18), level1WallShapes, null);
            _allActors.push(level1RightWall);            
            var level1Floor:ArbiStaticActor = new ArbiStaticActor(this, new Point(104, 502), level1FloorShapes, null);         
            _allActors.push(level1Floor);
            
         } else if (_level == 2) { etc etc etc.


That's probably pretty hard to read through without looking at the kerp.com tutorials, but essentially, I made the static graphics for the level in professional and then them as a class (Level1), and then in my draw level function which is invoked at various points, the code searches for an int variable which is increased and decreased as the player progresses through the levels of the game. The if function basically gets the level integer and then draws out the walls of the level ontop of the graphic which is added to the stage when the draw level function is invoked.

I dunno, it was a long time ago that I did this code, looking back at it now kinda makes me feel like there is a better way of doing things, I would watch a few of kerps tutorials at http://www.kerp.net/box2d/ on actors and the actor classes that he creates as they are a real helper in understanding how box2d deals with all of that sort of stuff.

Let me know how you go!


Thanks for the great advice! My second problem still remains, though. How am I supposed to add a preloader to each level stored in a movieclip?

Original thread: https://box2d.org/forum/viewtopic.php?t=7539