-2
Point bl = new Point(Guy.x, Guy.y + Guy.height);
Point br = new Point(Guy.x + Guy.width, Guy.y + Guy.height);
for(Tile p: platforms){
if (p.y == br.y && br.x >= p.x && bl.x <= p.x + p.width) {
isOnPlatform = true;
}else{
isOnPlatform = false;
}
}
当我运行那段代码时,控制台显示“NullPointerException”。为什么这不起作用?Java“foreach”无法正常工作... nullPointerException
我已经定义/初始化变量Tile[] platforms
。另外,当我运行它时,Guy
不会下降。
而且,我这样做的时候,它的工作原理:
//for(Tile p: platforms){
if (platforms[1].y == br.y && br.x >= platforms[1].x && bl.x <= platforms[1].x + platforms[1].width) {
isOnPlatform = true;
}else{
isOnPlatform = false;
}
// }
但是,我还有其他2 platforms
,我会添加像20多。我不想这样做,每个platforms
。
有什么想法?
_What是“NullPointerException”_...至少要付出一些努力。 –
NullPointerException发生在哪里(行号)?你可以在堆栈跟踪中看到它。 –
检查您的平台[0]元素 –