1
\$\begingroup\$

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:

Falling through the first level.

When I complete the first level - still good:

End of the first level.

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):

First player square failing.

Here is when the second player square has gone through on that very same second level, and it has disappeared as it should:

Second player square completing the level.

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ Juding by the edits, you seem to have found an answer. It's alright to answer your own questions, and much clearer if you do so in the answers section. (Some edits to clarify what exactly the problem was wouldn't hurt either…) \$\endgroup\$ – Anko Sep 1 '15 at 23:55
  • \$\begingroup\$ Thank you. I've just added an answer for it! I'm still kind of new to this, so thank you for explaining, I wasn't sure what the procedure was for answering my own problem. \$\endgroup\$ – 101_son Sep 2 '15 at 5:24
1
\$\begingroup\$

The problem was a body from the previous level seemed to be left behind for some reason in that spot in the screenshot from level two.

To correctly and definitely remove the bodies, I now do this:

// Loop through all of the current bodies in the world. 
for(int bodyNumber = 0; bodyNumber < world.getBodyCount(); bodyNumber++)
{
    // If the body still has a shape list.
    // Later versions of Box2D I think this is replaced with fixtures.
    if(world.getBodyList().getShapeList() != null)
    {
        // Destroy the shape list of the body.
        // Again, replaced with fixtures in later versions.
        world.getBodyList().destroyShape(world.getBodyList().getShapeList());
    }

    // Get the next body in the world body list.
    world.getBodyList().getNext();
}

The player can advance the level, and the first player square is no longer getting stuck!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.