2014-07-18 32 views
0

我在制作游戏,玩家不能离开屏幕。我试图做到这一点,如果玩家在屏幕一侧的相反方向前进,他们会让他们移动。但到目前为止,它只适用于屏幕的北侧和西侧。只有当玩家向上或向左移动时,移动才有效

运动方法:(每个玩家试图移动时调用):

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; 
     } 
    } 
} 
+2

你是通过代码?你认为问题在哪里发生? – krillgar

+0

当我让玩家移动时,这已经发生在我的方向enum(我不认为我必须展示它的代码,其中没有任何东西)。 – MCMastery

+1

请避免混合单行ifs和多行ifs,并在if/else链之间放置注释。你正在为自己设定一个很大的错误。此外,我有很大的麻烦理解你的代码,因为它是双重否定等等... – Pimgd

回答

1

北部和西部的核对Main.width /高度和他们的工作。其他两个没有。所以我猜

if (dir.equals(Direction.SOUTH) && loc.getY() <= mob.getSpeed()) 

不是你所期望的。

0

你的代码做了各种各样的东西,我不知道它做了什么。不过,我敢肯定,这

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(
(left && newLocation.topLeft.X >= 0) || 
(right && newLocation.bottomRight.X <= Main.width) || 
(up && newLocation.topLeft.Y >= 0) || 
(down && newLocation.bottomRight.Y <= Main.height) 
){ 
    return oldLocation; 
} 
else 
{ 
    return newLocation; 
} 

因为这对边界检查时,它是如何工作的。 如果你重构你的代码,你最终会发现这些东西。