So after much testing, I've revised my code (as seen below). Note that this code is used when the current level (scene) of the game ends and I want to set the next level (scene). Remember that I am reusing the same Activity and the same PhysicsWorld for all levels of my game.
First I declare some variables at the top of my activity. I make them public so that other classes can use them.
Using java Syntax Highlighting
public ArrayList<AnimatedSprite> dAnimatedSprite = new ArrayList<AnimatedSprite>();
public ArrayList<Shape> dShape = new ArrayList<Shape>();
Parsed in 0.034 seconds, using
GeSHi 1.0.8.4
Any time I create a new AnimatedSprite or new Shape, I also call one of the following:
Using java Syntax Highlighting
this.myActivity.dAnimatedSprite.add(myAnimatedSprite);
this.myActivity.dShape.add(myShape);
Parsed in 0.035 seconds, using
GeSHi 1.0.8.4
So at this point I have loaded ArrayLists with the objects I want to unload later. I will unload Body and Joint a different way (as you'll see below). When I'm ready to unload the current level (scene) of the game and go to the next level (scene) of the game, I do this:
Using java Syntax Highlighting
private void goToNextLevel(){
myGarbageCollection();
myLoadScene();
}
private void myGarbageCollection(){
Iterator<Body> allMyBodies = this.mPhysicsWorld.getBodies();
while(allMyBodies.hasNext())
{
try {
final Body myCurrentBody = allMyBodies.next();
this.runOnUpdateThread(new Runnable(){
@Override
public void run() {
mPhysicsWorld.destroyBody(myCurrentBody);
}
});
} catch (Exception e) {
Debug.d(e);
}
}
Iterator<Joint> allMyJoints = this.mPhysicsWorld.getJoints();
while(allMyJoints.hasNext())
{
try {
final Joint myCurrentJoint = allMyJoints.next();
this.runOnUpdateThread(new Runnable(){
@Override
public void run() {
mPhysicsWorld.destroyJoint(myCurrentJoint);
}
});
} catch (Exception e) {
Debug.d(e);
}
}
// check if the ArrayList contains anything
if(dAnimatedSprite.size()>0){
for(int i=0; i < dAnimatedSprite.size(); i++){
this.mScene.detachChild(dAnimatedSprite.get(i));
}
}
// unload the contents of the ArrayList
dAnimatedSprite.clear();
// check if the ArrayList contains anything
if(dShape.size()>0){
for(int i=0; i < dShape.size(); i++){
this.mScene.detachChild(dShape.get(i));
}
}
// unload the contents of the ArrayList
dShape.clear();
mScene.clearChildScene();
mScene.detachChildren();
mScene.reset();
mScene.detachSelf();
mPhysicsWorld.clearForces();
mPhysicsWorld.clearPhysicsConnectors();
mPhysicsWorld.reset();
mEngine.getFontManager().clear();
System.gc();
}
Parsed in 0.043 seconds, using
GeSHi 1.0.8.4
That code seems to work well.
What's interesting is while I was testing, I was getting WinDeath errors if I tried to destroy the Bodys and Joints without calling runOnUpdateThread. But I never got those errors when I tried to detach the AnimatedSprites and Shapes without calling runOnUpdateThread. In fact, I experienced some weird behavior if I tried to detach the AnimatedSprites and Shapes by calling runOnUpdateThread.
Anyway, like I said, the above code seems to work well. So you can feel free to try it out, and let me know if you have any comments or suggestions.
Thanks