문제

im developing a game using andangine and the tmx and body2d extensions. i create Objects(sprites) like coins an specific positions while creating my map. i use a contactlistener to check if the player collides with a coin.

how can i delete this sprite? and how can i organize my sprites best? thanks =)

도움이 되었습니까?

해결책

I assume you create a PhysicsConnector to connect between your sprites and bodies. Create a list of these physics connectors, and when you decide you should remove a body (And its sprite), do the following:

Body bodyToRemove = //Get it from your contact listener
for(PhysicsConnector connector : mPhysicsConnectors) //mPhysicsConnectors is the list of your coins physics connectors.
    if(connector.getBody() == bodyToRemove)
        removeSpriteAndBody(connector); //This method should also delete the physics connector itself from the connectors list.

About sprites organization: Coins are re-useable sprite, you shouldn't recreate them every time. You can use an object pool, here's a question about this topic.

다른 팁

I advice you to set user data of a body. And in your collision handler you would be able to work with it. Small example:

body.setUserData(...);

..

    public void postSolve(Contact contact, ContactImpulse impulse) {

            ... bodyAType = (...) bodyA.getUserData();
            ... bodyBType = (...) bodyB.getUserData();
            if (bodyAType != null && bodyBType != null) {
               if (bodyAType.getUserData.equals(...)) {
                   //.......do what you need
               }
            }
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top