AndEngine Sprite/Box2D Body removal crashes my program with no error/exception information?

心不动则不痛 提交于 2019-12-04 10:15:08

After googling about box2D and sprite/body removal it turns out you can't remove a sprite/body from the contactListener, but what you can do is set a flag in either the body or sprite to delete it and check for these flags in a seperate update method outside the contactListener. I did this by making a single 'makeUserData' method to create a JSONObject with the sprite/body/type and additionally a boolean 'deleteStatus' that determines if it flagged for deletion:

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) {
        // TODO Auto-generated catch block
        Log.d(TAG,"Exception creating user data:"+e);
    }
    return myObject;
}

Then instead of calling destroyObstruction() after collision I call this method i created to set the flag for deletion within the body to true:

private void setForDestruction(Body myBody) throws JSONException
{
    if(myBody.getUserData()!=null)
    {
        ((JSONObject)myBody.getUserData()).put("deleteStatus", true);
    }

}

Then in a seperate update handler (I had one in my onLoadScene method already to update the score) I added a call to another method I made to iterate through the bodies in my physics world looking for this flag:

 this.mScene.registerUpdateHandler(new IUpdateHandler() {
        @Override
        public void reset() { }

        @Override
        public void onUpdate(final float pSecondsElapsed) {
            //update the players score
            updateScore();

            //update the text on the screen
            playerScoreText.setText( "Score: "+PLAYER_SCORE);               
            playerLivesText.setText("Lives:"+PLAYER_LIVES);

            //remove any sprites flagged for deletion
            try{
                removeObjectsSetForDestruction();
            }catch(Exception e)
            {
                Log.d(TAG,"Exception removing objects from update:"+e);
            }
            catch(Error e)
            {
                Log.d(TAG,"Error removing objects from update:"+e);
            }

        }
    });

And here is the removeObjectsSetForDestruction method:

private void removeObjectsSetForDestruction()
{
    if(this.mPhysicsWorld!=null)
    {
        Iterator<Body> allMyBodies = this.mPhysicsWorld.getBodies();//gets all the bodies in my physics world
        boolean isDelete = false;
        JSONObject currentBodyData;
        while(allMyBodies.hasNext())
        {
             try {
//this code is in a try/catch bracket because some of my bodies don't have the extra data attached
                 currentBodyData = (JSONObject)allMyBodies.next().getUserData();//gets the next JSONOBject from the body
                 if(currentBodyData!=null)
                 {
                     isDelete = (Boolean) currentBodyData.get("deleteStatus");
                    if(isDelete)
                    {
                        destroyObstruction((Body) currentBodyData.get("body"));
                    }
                 }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                Log.d(TAG,"Error getting world bodies data:"+e);
            }
        }
    }
}

EDIT: The AndEngine wiki on box2D explains pretty well how the world physics calculation is fragile so you need to be really careful when adding/deleting/moving bodies as in some places it could happen at the same time as the world physics calculation, which eventually causes the program to crash. It also outlines a solution which is to place the code into 'this.runOnUpdateThread'. So for example in my code when I added an obstruction sprite in my code (they are added from a CountDownTimer so the chances that they could be added at the same time as world step calculation is likely) I wrapped it in a thread:

private void addObstruction(final float pX, final float pY) {

    runOnUpdateThread(new Runnable() {



    @Override
    public void run() {
        final Body body;
        final Sprite myObstruction;

        myObstruction = new Sprite(pX, pY-mCrateTextureRegion.getHeight(), mCrateTextureRegion);

        body = PhysicsFactory.createBoxBody(mPhysicsWorld, myObstruction, BodyType.DynamicBody, OBJECT_FIXTURE_DEF);
        //body.setLinearDamping(10);
        //body.setAngularDamping(10);
        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(myObstruction, body, true, true));

        body.setUserData(makeUserData(OBSTRUCTION_TYPE,body,myObstruction));
        myObstruction.setUserData(makeUserData(OBSTRUCTION_TYPE,body,myObstruction));

        myObstruction.registerUpdateHandler(new IUpdateHandler() {

            @Override
            public void reset() {

            }

            @Override
            public void onUpdate(float pSecondsElapsed) {
                    runOnUpdateThread(new Runnable() {

                    @Override
                    public void run() {
                        final Vector2 velocity = Vector2Pool.obtain(-10f, 0f);
                        body.setLinearVelocity(velocity);
                        Vector2Pool.recycle(velocity);

                    }
                });

            }
        });
        //attach our Obstruction to the scene
        mScene.attachChild(myObstruction);

    }

    });
}

I've used these threads in most places I do code with bodies and I can confirm this stopped my random crashes :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!