我已经尝试设置我自己的类来处理冲突,但它似乎只是输出下面的错误。程序打开很好,它只在球和尖峰碰撞时输出错误。感谢
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.alexcz.mariobros.Tiles.HandleCollisions.ballSpike(HandleCollisions.java:20)
at com.alexcz.mariobros.Tools.WorldContactListener.beginContact(WorldContactListener.java:41)
at com.badlogic.gdx.physics.box2d.World.beginContact(World.java:982)
public class HandleCollisions {
Character player;
World world;
public HandleCollisions(Character player, World world) {
this.player = player;
this.world = world;
}
public void ballSpike(){
player.hitSpike();
System.out.println("collided");
}
}
public class WorldContactListener implements ContactListener {
public World world;
public Character player;
public HandleCollisions handleCollisions;
public WorldContactListener(Character player,World world) {
world = this.world;
player = this.player;
handleCollisions = new HandleCollisions(player, world);
}
@Override
public void beginContact(Contact contact) {
Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();
if(fixtureA.getUserData() == "ball" || fixtureB.getUserData() == "ball"){
Fixture ball = fixtureA.getUserData() == "head" ? fixtureA : fixtureB;
Fixture object = ball == fixtureA ? fixtureB : fixtureA;
if(object.getUserData() instanceof InteractiveTileObject)
{
((InteractiveTileObject)object.getUserData()).hit();
handleCollisions.ballSpike();
}
}
}
答案 0 :(得分:1)
这是您的问题:
world = this.world; // this.world is null
player = this.player; // this.player is null
应该是:
this.world = world;
this.player = player;
因为this
是您的实例,所以您将如何分配类变量。
我猜这就是为什么你的handleCollisions
是null
,并在执行java.lang.NullPointerException
时抛出handleCollisions.ballSpike()
。
这里有一个调试检查器派上用场。