So developing for an android mobile game, using Android Studio and developing for the API 17, with Box2D implemented.
FIRST LEVEL
For some reason, one the first level everything is running okay:
When I complete the first level - still good:
SECOND LEVEL
Here is when the problem occurs, on the second level, as you can see the first player square (the upper, smaller square) to the left is not disappearing along with the target square (the bottom, bigger square):
Here is when the second player square has gone through on that very same second level, and it has disappeared as it should:
My process for transitioning between levels (this function is in my level class):
public void newLevel()
{
// Clear the current level.
levelGenerator.clearLevel();
// Increment the current level number.
levelNumber++;
// Decide what levels I want here.
if(levelNumber == 2)
{
// Generate a new level.
levelGenerator = new LevelGenerator
(
context, // Game context.
world, // Box2D world.
background, // Game background, relative layout in this case.
screenWidth, // The screen width of the device currently being used.
screenHeight, // The screen height of the device currently being used.
3, // The number of columns.
7, // The maximum number of platforms (both moveable and breakable) per column.
1 // The number of enemies.
);
}
// Builds the new level.
levelGenerator.buildLevel();
// Updates the UI thread to add all of the new level objects to the screen.
game.render();
}
Clear The Current Level
- Remove any textures/colours (set the alpha to 0 for colours) from the level objects.
- Destroy the body of each object in the level objects vector. (A vector which stores animated sprites.).
Finally, remove all the elements from the level objects vector.
public void clearLevel() { // If there are objects in the vector. if(!objects.isEmpty()) { // Iterate through each of the objects in the level. for(AnimatedSprite object : getObjects()) { // If there is an object. if(object != null) { // If the object is an enemy. if((object.getID() == ObjectID.STATICENEMY) || (object.getID() == ObjectID.DYNAMICENEMY)) { // Take care of the bullets separately. // Enemy bullets are only in the enemy class and are not part of the level objects vector. Enemy enemy = (Enemy) object.body.getUserData(); enemy.enemyBullet.setColour(0, 0, 0, 0); enemy.enemyBullet.destroyBody(); } // Removing all of the sprites from the screen. // If the object does not have a texture, but is using standard colours. if(object.colour != null) { // Set the alpha value to 0 and RGB to 0. object.setColour(0, 0, 0, 0); } // Otherwise, the object has a texture. else if(object.image != null) { // Set the texture to be transparent. object.removeTexture(); } // If the object still has a body. if(object.body != null) { // // Problem may be here??? // Screenshot may be showing a body in the way of the target square? // // Destroy the body of the object. object.destroyBody(); } } } // Remove all of the objects from the list. objects.removeAllElements(); } }
Here is how I am destroying the bodies (in my animated sprite class):
public void destroyBody() { body.destroyShape(body.getShapeList()); }
Increment The Level Number
- Will be random levels, but for testing/debugging purposes, using set levels.
Check The Level Number
- Generate the new level using my level generator class.
Put The Level Together And Populate The Level Objects Vector
- This will add in all of the new level objects, using the values specified in the constructor for level generation.
When I checked the size of the vector after the clear function, it was all as expected, and the same for when a new level was created.
When debugging through the second level and monitoring collisions, I use contact listeners from Box2D, I did find that the first player square was always colliding with a column, I do not know why, here is the log entry:
First player square:
08-30 10:50:46.636 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 6 08-30 10:50:46.636 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 6 08-30 10:50:46.656 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 6 08-30 10:50:46.656 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 6 08-30 10:50:46.785 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 6 08-30 10:50:46.785 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 6
Second player square:
08-30 10:50:46.935 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 5 08-30 10:50:46.935 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 5 08-30 10:50:47.226 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 5 08-30 10:50:47.226 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 5 08-30 10:50:47.406 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 5 08-30 10:50:47.416 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 5 08-30 10:50:47.566 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 5 08-30 10:50:47.646 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 5 08-30 10:50:47.696 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 5 08-30 10:50:47.696 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 5 08-30 10:50:47.746 15664-15770/com.example.app.jason.jasons_app D/TKT﹕ Player colliding with 5
public class ObjectID
{
//
// Other ID tags.
//
// The ID's from the log entry.
public static final int MOVEABLEPLATFORM = 5;
public static final int COLUMN = 6;
}
Does anyone have an idea why the first player square, when I create a new level, ONLY registers collisions with my columns? I have no idea and I would really appreciate any help. If you need any further clarification please comment/message me.
Thank you.
[EDIT]
So I have fixed my initial problem mentioned above! For anyone wondering it was to do with how I am deleting/removing bodies from the world.
A body, or multiple bodies were not being fully removed correctly, and were carrying over to the next level.
My answer to the problem is below.