Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6545307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:34:13+00:00 2026-05-25T11:34:13+00:00

I am making a game using Andengine/Box2D physics addon. I experienced the random crashes

  • 0

I am making a game using Andengine/Box2D physics addon. I experienced the random crashes due to the addition/movement/deletion of box2d bodies during the world step calculation, so I have implemented code to flag sprites/bodies for removal using the setUserData method – I attach a JSON object to each body and sprite that contains the type of sprite, the body, and the sprite itself and its delete status:

private JSONObject makeUserData(int type, Body body, Object sprite)
{
    JSONObject myObject = new JSONObject();

    try {
        myObject.put("type", type);
        myObject.put("sprite", sprite);
        myObject.put("body", body);
        myObject.put("deleteStatus", false);
    } catch (JSONException e) {
        Log.d(TAG,"Exception creating user data:"+e);
    }
    return myObject;
}

then in an update thread iterate through all the bodies in my world looking for these flags and delete the sprites/bodies with the flag. The bodies remove correctly, however the sprite removal seems to delete every instance of that particluar sprite rather than just removing the particular one i flagged to remove! I can tell the bodies are still present without the sprite as my player collides with invisible objects! Here is the code for removal:

 private void removeObjectsSetForDestruction()
{
    if(this.mPhysicsWorld!=null)
    {
        Iterator<Body> allMyBodies = this.mPhysicsWorld.getBodies();
        boolean isDelete = false;
        JSONObject currentBodyData;
        while(allMyBodies.hasNext())
        {
             try {
                 currentBodyData = (JSONObject)allMyBodies.next().getUserData();
                 if(currentBodyData!=null)
                 {
                     isDelete = (Boolean) currentBodyData.get("deleteStatus");
                    if(isDelete)
                    {
                        destroyObstruction((Body) currentBodyData.get("body"));
                    }
                 }
            } catch (JSONException e) {
                Log.d(TAG,"Error getting world bodies data:"+e);
            }
        }
    }
}

private void destroyObstruction(Body obstructionBody) throws JSONException
{
    obstructionBody.setActive(false);

        JSONObject secondBodyData = null;
        if(obstructionBody.getUserData()!=null)
        {
            secondBodyData=(JSONObject) obstructionBody.getUserData();

            //explodeObstruction(((IEntity) secondBodyData.get("sprite")).getX(),((IEntity) secondBodyData.get("sprite")).getY());
            if(secondBodyData.get("sprite") instanceof AnimatedSprite)
            {
                removeObject((AnimatedSprite) secondBodyData.get("sprite"));
            }
            else
            {
                removeObject((Sprite) secondBodyData.get("sprite"));
            }
        }


}

private void removeObject(final AnimatedSprite myAnimSprite)
{
    final PhysicsConnector myPhysicsConnector = this.mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(myAnimSprite);

    this.mPhysicsWorld.unregisterPhysicsConnector(myPhysicsConnector);
    this.mPhysicsWorld.destroyBody(myPhysicsConnector.getBody());

    this.mScene.unregisterTouchArea(myAnimSprite);
    this.mScene.detachChild(myAnimSprite);

    System.gc();
}

private void removeObject(final Sprite mySprite)
{
    final PhysicsConnector myPhysicsConnector = this.mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(mySprite);

    this.mPhysicsWorld.unregisterPhysicsConnector(myPhysicsConnector);
    this.mPhysicsWorld.destroyBody(myPhysicsConnector.getBody());

    this.mScene.unregisterTouchArea(mySprite);
    this.mScene.detachChild(mySprite);

    System.gc();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:34:14+00:00Added an answer on May 25, 2026 at 11:34 am

    I would like to take a look at your objects creating code. I assume every sprite has the same TextureRegion used, so when the region of one sprite is being changed – same regions on other sprites are being changed too due to Android architecture specifics. For every sprite with same TextureRegion you should use textureRegion.clone() as the last parameter of the constructor. Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Box2d for a Bike Physics Game, Box2d lets you have fixtures that
okay i'm making a game using c++ (for the engine) and openGL, now i've
I am making a game using C# with the XNA framework. The player is
I'm making a game using JigLib and GLGE . The camera follows the player
I am currently in the process of making a snake game using VB.NET... I
Little background.. I'm in the process of making an OpenGL game using Java and
I'm making a game on iPhone using cocos2d, and I have a question. In
I'm making a small game using the HTML5 canvas element. It works great, except
I'm making a game in html5 canvas. I'm using jquery so I can get
I am making a game in JAVA where I want to come up with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.