我在制作游戏,玩家不能离开屏幕。我试图做到这一点,如果玩家在屏幕一侧的相反方向前进,他们会让他们移动。但到目前为止,它只适用于屏幕的北侧和西侧。只有当玩家向上或向左移动时,移动才有效
运动方法:(每个玩家试图移动时调用):
public static Location move(Direction dir, Mob mob, boolean checkRoomBounds, int distance) {
Location loc = mob.getLocation();
Location location = new Location();
location.setX(loc.getX());
location.setY(loc.getY());
if (dir.equals(Direction.WEST))
location.setX(loc.getX() - distance);
else if (dir.equals(Direction.EAST))
location.setX(loc.getX() + distance);
else if (dir.equals(Direction.NORTH))
location.setY(loc.getY() - distance);
else if (dir.equals(Direction.SOUTH))
location.setY(loc.getY() + distance);
else
// return original location, so it doesn't move
return loc;
if (!checkRoomBounds)
return location;
else {
// Crop the dimension so that the mob doesn't go fully out at the right side
// of the screen and it goes not fully out on the left side
Dimension mobCropped = DimensionGenerator.generate(mob.getLocation(),
mob.getSprite().getWidth(null), mob.getSprite().getHeight(null));
Location origTopLeft = mobCropped.getTopLeft();
Location origBottomRight = mobCropped.getBottomRight();
Location newTopLeft = new Location(origTopLeft.getX() +
mob.getSprite().getWidth(null), origTopLeft.getY() +
mob.getSprite().getHeight(null));
Location newBottomRight = new Location(origBottomRight.getX() -
mob.getSprite().getWidth(null), origBottomRight.getY() -
mob.getSprite().getHeight(null));
mobCropped.setTopLeft(newTopLeft);
mobCropped.setBottomRight(newBottomRight);
// if the new location is outside the room
if (!Collision.detect(mobCropped, Main.roomDimension)) {
// if player is going away from wall but they are outside the room, allow them to
// go away
if (dir.equals(Direction.WEST) && loc.getX() >= Main.width)
return location;
if (dir.equals(Direction.EAST) && loc.getX() <= mob.getSpeed())
return location;
if (dir.equals(Direction.NORTH) && loc.getY() >= Main.height)
return location;
if (dir.equals(Direction.SOUTH) && loc.getY() <= mob.getSpeed())
return location;
// if they aren't moving away from the wall, dont let them move
return loc;
}
// if they aren't outside the room
else {
return location;
}
}
}
你是通过代码?你认为问题在哪里发生? – krillgar
当我让玩家移动时,这已经发生在我的方向enum(我不认为我必须展示它的代码,其中没有任何东西)。 – MCMastery
请避免混合单行ifs和多行ifs,并在if/else链之间放置注释。你正在为自己设定一个很大的错误。此外,我有很大的麻烦理解你的代码,因为它是双重否定等等... – Pimgd