我正在制作一个简单的 Java 游戏作为我学习它的一部分,当我运行它时,我得到:
Exception in thread "LWJGL Application" java.lang.NullPointerException at com.mattihase.floatingpoint.GameScreen.generalUpdate(GameScreen.java:65) at com.mattihase.floatingpoint.GameScreen.render(GameScreen.java:42) at com.badlogic.gdx.Game.render(Game.java:46) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
这是它所讨论的一般更新行及其下面的行:
if(pc.bounds.overlaps(gary.Rbounds))
//where i test a bunch of booleans to see what the dialog returned should be
//yes, i know i'm probably doing the ifs it unoptimized but i couldn't get it to
//work the other way.
//the pc.bounds stuff is there to make it follow the player character around the
//world as the camera already does this.
{
if(pc.onquest = false){
if(pc.finishquest = false){
Assets.font.draw(batch, gary.Gdio, pc.bounds.x - 250, pc.bounds.y - 190);
pc.onquest = true;
}
}
if(pc.onquest = true){
if(pc.finishquest = false){
Assets.font.draw(batch, gary.GDdio, pc.bounds.x - 250, pc.bounds.y - 190);
}
}
if(pc.haspoints = true){
pc.finishquest = true;
}
if(pc.finishquest = true);
Assets.font.draw(batch, gary.GCdio, pc.bounds.x - 250, pc.bounds.y - 190);
pc.onquest = false;
pc.award = true;
}`
这些是玩家角色和 npc 的类(分别称为 pc(类 PlayerCharacter)和加里(类 Rectoid)) 玩家角色
public Sprite image;
public Rectangle bounds;
public int score;
public String scoreprint;
public boolean onquest;
public boolean finishquest;
public boolean haspoints;
public boolean award;
public PlayerCharacter(){
onquest = false;
haspoints = false;
finishquest = false;
award = false;
score = 0;
image = Assets.s_pc;
bounds = new Rectangle(256-16, 192-16, 16, 16);
scoreprint = score + "";`
还有加里 `公共(public) Sprite Rimage; 公共(public)矩形边界; 公共(public)字符串Gdio; 公共(public)字符串GDdio; 公共(public)字符串GCdio;
public Rectoid()
{
Rimage = Assets.s_rectoid;
Rbounds = new Rectangle(1024, 64, 64, 32);
Gdio = "the diolog";
GDdio = "the diolog";
GCdio = "the diolog";`
c
最佳答案
在 Java 中,要检查对象引用或基本类型的相等性,您必须使用 ==
运算符。在像 if(pc.onquest = false){
这样的行中,您将 false
分配给 onquest
,而不是检查其值。
代码之所以能够编译通过,是因为在Java中赋值会返回指定的值。
例如这句话:
int zero = 0;
if (zero == 0){} //its true
if (zero = 0){} //compile error, becouse 'zero = 0' returns '0', the asigned value
您的代码中发生的情况是您正在使用 boolean 变量。
if(pc.bounds.overlaps(gary.Rbounds))
{
if(pc.onquest = false){ // HERE YOU ARE DOING TWO THINGS: assing false to
// onquest and return false. The following code is never execute
if(pc.finishquest = false){ // SAME HERE
Assets.font.draw(batch, gary.Gdio, pc.bounds.x - 250, pc.bounds.y - 190);
pc.onquest = true;
}
}
if(pc.onquest = true){// SAME HERE
if(pc.finishquest = false){// SAME HERE
Assets.font.draw(batch, gary.GDdio, pc.bounds.x - 250, pc.bounds.y - 190);
}
}
if(pc.haspoints = true){// SAME HERE
pc.finishquest = true;
}
if(pc.finishquest = true);// SAME HERE
Assets.font.draw(batch, gary.GCdio, pc.bounds.x - 250, pc.bounds.y - 190);
pc.onquest = false;
pc.award = true;
}`
此外,要检查 boolean 值,只需使用它,而不是将其与 true 或 false 进行比较,并使用 !
运算符。
如果你想检查 onquest 是否被禁用,只需简单地使用 if(!pc.onquest){
即可。
关于NullPointerEception,可能gary变量为null。检查您之前是否创建过它。它应该是这样的:
Rectoid gary = new Rectoid();
关于java - libgdx 中重叠时出现 NullPointerException (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26455632/