2013-05-29 40 views
0

一旦遇到窗口边缘,我的角色就​​会停下来。 这是我的更新方法。点击窗口边缘时的字符碰撞

public void update(GameContainer gc, StateBasedGame sbg, int delta) 
    { 
     Input input = gc.getInput(); 
     playerX += VelocityX; 

     gc.setShowFPS(Splash.showFps); 

     if(input.isKeyPressed(Input.KEY_F1)) 
     { 
      Splash.showFps = !Splash.showFps; 
     } 

     if (input.isKeyDown(Input.KEY_RIGHT)) 
      VelocityX = 10; 
     else if (input.isKeyDown(Input.KEY_LEFT)) 
      VelocityX = -10; 
     else if (playerX >= 700) 
      VelocityX = 0; 
     else 
     { 
      VelocityX = 0; 
     } 

    } 

我意识到去上向左发生,因为我还没有编码它尚未但性格熄灭屏幕右侧

回答

1
if (input.isKeyDown(Input.KEY_RIGHT)){ 
     VelocityX = 10;} 
    else if (input.isKeyDown(Input.KEY_LEFT)){ 
     VelocityX = -10;} 
    else{VelocityX = 0;} 
    if (playerX >699){ 
     playerX=699; 
     VelocityX = 0;} 
    else if(playerX<1){ 
     playerX=1;VelocityX = 0; 
     } 
+0

我之前尝试过类似的东西,但它的工作原理,但一旦停在边缘,角色根本无法移动。 – freemann098

+0

@ freemann098编辑 – Nikki

0

问题修复。在关键检测你的速度设置为0

所以不是

if (input.isKeyDown(Input.KEY_RIGHT)) 
      VelocityX = 10; 
     else if (playerX >= 700) 
      VelocityX = 0; 

这样做

if (input.isKeyDown(Input.KEY_RIGHT)) 
     { 
      VelocityX = 10; 
      if (playerX >= 700) 
       VelocityX = 0; 
     } 
0

有几件事情错了,我注意到了。首先是你的更新问题:你想让玩家按他们的速度移动。你还需要做你的边界检查。另一个问题是,其他人没有注意到的是,你有一个正确的按键偏好。这意味着如果你拿着右键和左键,玩家就会右移。这是由于if/else if语句。你应该分开你的IFS出来的控制更精细的级别:

public void update(GameContainer gc, StateBasedGame sbg, int delta) 
{ 
    Input input = gc.getInput(); 
    //playerX += VelocityX; -> this is moved 

    gc.setShowFPS(Splash.showFps); 

    if(input.isKeyPressed(Input.KEY_F1)) 
    { 
     Splash.showFps = !Splash.showFps; 
    } 

    VelocityX = 0; 
    if (input.isKeyDown(Input.KEY_RIGHT)) 
     VelocityX += 10; 
    //removed the else 
    if (input.isKeyDown(Input.KEY_LEFT)) 
     VelocityX -= 10; 

    //you want to bounds check regardless of the above statments 
    //get rid of the else 
    if ((playerX >= 700 && VelocityX > 0) || (playerX <= 0 && VelocityX < 0)) //check both sides 
     VelocityX = 0; 

    //move the player 
    playerX += VelocityX; 
} 

编辑:修改我的代码来解决无法同时边界时移动的问题。