【问题标题】:AndEngine Sprite/Box2D Body removal removes a particluar body (as it should), but removes all instances of the sprite?AndEngine Sprite/Box2D Body 移除移除了一个特定的身体(因为它应该),但移除了精灵的所有实例?
【发布时间】:2011-09-09 09:45:15
【问题描述】:

我正在使用 Andengine/Box2D 物理插件制作游戏。由于在世界步计算期间添加/移动/删除 box2d 主体,我经历了随机崩溃,因此我实现了代码以使用 setUserData 方法标记精灵/主体以删除 - 我将 JSON 对象附加到每个主体和精灵包含精灵的类型、主体、精灵本身及其删除状态:

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;
}

然后在更新线程中遍历我世界中的所有物体以查找这些标志并删除带有标志的精灵/物体。身体删除正确,但是精灵删除似乎删除了该特定精灵的每个实例,而不仅仅是删除我标记要删除的特定实例!当我的玩家与不可见的物体发生碰撞时,我可以看出没有精灵的身体仍然存在!以下是删除代码:

 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();
}

【问题讨论】:

    标签: android box2d sprite andengine


    【解决方案1】:

    我想看看您的对象创建代码。我假设每个精灵都使用相同的TextureRegion,因此当一个精灵的区域被更改时 - 由于 Android 体系结构的特殊性,其他精灵的相同区域也被更改。对于每个具有相同TextureRegion 的精灵,您应该使用textureRegion.clone() 作为构造函数的最后一个参数。希望这会有所帮助。

    【讨论】:

    • 感谢您的指点,我会试一试 :) 可能值得注意的是,我认为代码已从 textureRegion.clone() 更改为 textureRegion.deepCopy()(至少在我的代码中是这样的)我认为是最新版本的andengine :S)
    • 看起来 textureRegion.deepCopy() 成功了 :) 再次感谢您的指点!
    • @AndroidNoob,不客气!看来我需要更新我的 AndEngine 库! =)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 2019-07-31
    • 2012-11-27
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    相关资源
    最近更新 更多