I'm still new to Java and Android programming and I have so many problems. Deleting an object in a collision. I inspected the web interface and found that I should not handle the removal of BOX2D bodies during conflict detection (contact listener), and I should add my objects to arraylist and set a variable in the User Data section of the body to remove or not process the action deletion in the update handler. So I did this: First, I define two ArrayLists, one for faces and one for bodies:
ArrayList<Sprite> myFaces = new ArrayList<Sprite>(); ArrayList<Body> myBodies = new ArrayList<Body>();
Then, when I create a face and connect it to his body, I add them to my ArrayLists as follows:
face = new AnimatedSprite(pX, pY, pWidth, pHeight, this.mBoxFaceTextureRegion); Body BoxBody = PhysicsFactory.createBoxBody(mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef); mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, BoxBody, true, true)); myFaces.add(face); myBodies.add(BoxBody);
I now add a contact listener and update handler in onloadscene as follows:
this.mPhysicsWorld.setContactListener(new ContactListener() { private AnimatedSprite face2; @Override public void beginContact(final Contact pContact) { } @Override public void endContact(final Contact pContact) { } @Override public void preSolve(Contact contact,Manifold oldManifold) { } @Override public void postSolve(Contact contact,ContactImpulse impulse) { } }); scene.registerUpdateHandler(new IUpdateHandler() { @Override public void reset() { } @Override public void onUpdate(final float pSecondsElapsed) { } });
My plan is to find out which two bodies collided in the contact listener by checking the variable from the body user data section, get their numbers in the list of arrays, and finally use the update handler to delete these bodies.
Questions: Am I using arraylist correctly? and in the conflict listener, how to retrieve an object that collided with an array? How to add a variable to user data (code, please). I tried to remove the body in this update handler, but it still throws me a NullPointerException, so this is the right way to add an update handler and where to add it? Any other tips for this would be great. Thanks in advance.