我的代码是:
public void move() {
moveX();
moveY();
}
public void moveX() {
if(xMove > 0) {
int tx = (int) (x + xMove + bounds.x + bounds.width) / Tile.TILEWIDTH;
if(!collisionWithTile(tx, (int) (y + bounds.y) / Tile.TILEHEIGHT) && !collisionWithTile(tx, (int) (y + bounds.y + bounds.height) / Tile.TILEHEIGHT)) {
x += xMove;
}
} else if(xMove < 0) {
int tx = (int) (x + xMove + bounds.x) / Tile.TILEWIDTH;
if(!collisionWithTile(tx, (int) (y + bounds.y) / Tile.TILEHEIGHT) && !collisionWithTile(tx, (int) (y + bounds.y + bounds.height) / Tile.TILEHEIGHT)) {
x += xMove;
}
}
}
public void moveY() {
if(yMove < 0) {
int ty = (int) (y + yMove + bounds.y + bounds.height) / Tile.TILEHEIGHT;
if(!collisionWithTile((int) (x + bounds.x) / Tile.TILEHEIGHT, ty) && !collisionWithTile((int) (x + bounds.x + bounds.width) / Tile.TILEWIDTH, ty)) {
y += yMove;
}
}
if(yMove > 0) {
int ty = (int) (y + yMove + bounds.y) / Tile.TILEHEIGHT;
if(!collisionWithTile((int) (x + bounds.x) / Tile.TILEHEIGHT, ty) && !collisionWithTile((int) (x + bounds.x + bounds.width) / Tile.TILEWIDTH, ty)) {
y += yMove;
}
}
}
protected boolean collisionWithTile(int x, int y) {
return room.getTile(x, y).isSolid();
}
但是当我运行它时,我得到了这个StackTrace的NullPointerException:
Exception in thread "Thread-0" java.lang.NullPointerException
at com.pixelandbitgames.entities.creature.Creature.collisionWithTile(Creature.java:69)
at com.pixelandbitgames.entities.creature.Creature.moveX(Creature.java:41)
at com.pixelandbitgames.entities.creature.Creature.move(Creature.java:27)
at com.pixelandbitgames.entities.creature.Player.tick(Player.java:19)
at com.pixelandbitgames.states.GameState.tick(GameState.java:23)
at com.pixelandbitgames.game.Game.tick(Game.java:72)
at com.pixelandbitgames.game.Game.run(Game.java:113)
at java.lang.Thread.run(Thread.java:748)
我看不到这是哪里来的,因为crashWithTile已填满所有参数。如果有人可以帮助我,那将是可取的。如果有人需要其他任何帮助,我会提供。我的其他班级似乎还不错,仅此而已。当我尝试移动时会发生这种情况。我的Room.getTile代码在这里:
public Tile getTile(int x, int y) {
if(x < 0 || y < 0 || x >= width || y >= height)
return Tile.floorTile;
Tile t = Tile.tiles[tiles[x][y]];
if(t == null)
return Tile.floorTile;
return t;
}
,这无法返回null。除非被覆盖,否则solid只是返回true。
public boolean isSolid() {
return false;
}
答案 0 :(得分:-1)
room
为 null ,或者room.getTile(x, y)
返回 null 。您尚未粘贴其中的相关内容,因此无法提供更多信息。