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:
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.
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.
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;
}
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);
}
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.