I have done a test but I receipt a NullPointerException. I don’t now why receipt this, so, I have the contact listener in a class and in the Screen class I put this method: (and connect it to the create method)
public void testContact(){
BodyDef bdtest = new BodyDef();
bdtest.type = BodyType.StaticBody;
bdtest.position.set(new Vector2(5, 5));
btest = world.createBody(bdtest);
CircleShape cs = new CircleShape();
cs.setRadius(0.5f);
ftest = btest.createFixture(cs, 0.6f);
//NullPointerExeption
test = new Test(instance, player.fplayer, ftest);
world.setContactListener(test);
}
This is the NullPointerException:
java.lang.NullPointerException
at com.unclain.udevelop.prueba1.ScreenTestOne.testContact(ScreenTestOne.java:301)
at com.unclain.udevelop.prueba1.ScreenTestOne.show(ScreenTestOne.java:125)
at com.badlogic.gdx.Game.setScreen(Game.java:62)
at com.unclain.udevelop.prueba1.GameScreen$1.onEvent(GameScreen.java:71)
at aurelienribon.tweenengine.BaseTween.callCallback(BaseTween.java:380)
at aurelienribon.tweenengine.BaseTween.updateStep(BaseTween.java:521)
at aurelienribon.tweenengine.BaseTween.update(BaseTween.java:424)
at aurelienribon.tweenengine.TweenManager.update(TweenManager.java:166)
at com.unclain.udevelop.prueba1.GameScreen.render(GameScreen.java:37)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:187)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110)
This is the ContactListener class:
package com.unclain.udevelop.prueba1.contacts;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.unclain.udevelop.prueba1.GameScreen;
import com.unclain.udevelop.prueba1.ScreenTestOne;
import com.unclain.udevelop.prueba1.UDevelop1;
import com.unclain.udevelop.prueba1.ingame.EntityPlayer;
public class Test implements ContactListener{
private Fixture fA;
private Fixture fB;
private ScreenTestOne mainInstance;
public Test(ScreenTestOne mainInstance, Fixture fA, Fixture fB){
this.mainInstance = mainInstance;
this.fA = fA;
this.fB = fB;
mainInstance = new ScreenTestOne(new GameScreen(new UDevelop1()));
}
@Override
public void beginContact(Contact contact) {
// TODO Auto-generated method stub
fA = contact.getFixtureA();
fB = contact.getFixtureB();
if(fA.getBody() != null && fB.getBody() != null){
if(fA.equals(mainInstance.player.fplayer) && fB.equals(mainInstance.ftest)){
System.out.println("¡QUESO!");
}
}
}
@Override
public void endContact(Contact contact) {
// TODO Auto-generated method stub
fA = contact.getFixtureA();
fB = contact.getFixtureB();
if(fA.getBody() != null && fB.getBody() != null){
if(fA.equals(mainInstance.player.fplayer) && fB.equals(mainInstance.ftest)){
System.out.println("¡QUESO!");
}
}
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
// TODO Auto-generated method stub
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
// TODO Auto-generated method stub
}
}
I was searching but I don’t find nothing useful, so, How work ContactListener? How can I detect a contact between two Fixtures?
Thanks.
P.D: I’m spanish, so, thanks for try understand me. ;D