我试图使用eclipse将Android游戏移植到Android,但是,我试图调试碰撞检测系统。我为游戏的碰撞检测系统使用了“java.awt.Rectangle”,并且它在Applet上运行良好,但是当我将它转换为“android.graphics.Rect”时,应用程序会在logcat中返回这个值:如何调试Android Rect类的空指针异常
09-24 16:23:07.047:E/AndroidRuntime(15845):java.lang.NullPointerException:尝试从空字段'int android.graphics.Rect.left'读取null对象引用
有人可以告诉我这意味着什么,如何调试,以及我的代码有什么问题? (如下所示)
编辑:进行更改后,我发现“Rect.intersects”不能正常工作。两个矩形不会识别碰撞。
如果有帮助,我是从 “http://www.kilobolt.com/day-7-creating-an-android-game-from-start-to-finish.html”
private int x;
private int y;
private int speedY;
protected boolean visible;
protected Rect r;
public Shoot(int startX, int startY) {
// TODO Auto-generated constructor stub
x = startX;
y = startY;
speedY=-14;
visible= true;
r = new Rect(0, 0, 0, 0);
}
public void update() {
y += speedY;
r.set(x, y, 15, 15);
if (y < 0) {
visible = false;
r = null;
}
else if (y < 750) {
checkCollision();
}
}
private void checkCollision() {
//detects collision
if (Rect.intersects(r, GameScreen.basket.getBounds())) {
visible = false;
GameScreen.score += 1;
}
}
(下面的代码是从篮筐类)
public Rect getBounds() {
//Creates Rectangle boundaries for collisions
return new Rect(240, 73, 15, 15);
}
感谢您的答复。这删除了空指针异常,但现在碰撞检测根本不起作用。对象不会消失,分数也不会增加。 – master248 2014-09-26 17:53:45
碰撞检测适用于y> 0和y <750,你不需要这样吗? – sotcha 2014-09-26 18:00:24
由于某些原因,碰撞检测对我而言并不起作用。两个物体遇到什么都不会发生。 – master248 2014-09-26 20:29:04