由于某种原因,我的播放器对象在释放移动键后继续移动。速度保持在玩家的移动速度,直到释放按键并再次释放。在查看velX和velY的控制台输出时,我看到它在按住几秒后将每个帧从8切换到16。并且在释放钥匙之后,无论如何,速度将保持在8或-8。
//Player tick method
public void tick() {
//Dissalow Exceess Velocity
velX = Calculations.clamp(velX, -getMaxSpeed(), getMaxSpeed());
velY = Calculations.clamp(velY, -getMaxSpeed(), getMaxSpeed());
//move the player
x += velX;
y += velY;
//dissalow exiting room
x = Calculations.clamp(x, 0, Game.WIDTH - 32);
y = Calculations.clamp(y, 0, Game.HEIGHT - 32);
//check for collisions with objects
collision();
}
这是我上面使用的Clamp方法(Calculations.clamp)
//clamp
public static int clamp(int var, int min, int max)
{
if(var >= max)
return var = max;
else if(var <= min)
return var = min;
else return var;
}
最后我的键盘输入法
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class KeyInput extends KeyAdapter
{
private Handler myHandler;
public KeyInput(Handler handler)
{
this.myHandler = handler;
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
for(GameObject object : myHandler.objects)
{
if(object.getId() == ID.player)
{
if(key == KeyEvent.VK_W) object.setVelY(object.getVelY() - object.getMaxSpeed());
if(key == KeyEvent.VK_S) object.setVelY(object.getVelY() + object.getMaxSpeed());
if(key == KeyEvent.VK_A) object.setVelX(object.getVelX() - object.getMaxSpeed());
if(key == KeyEvent.VK_D) object.setVelX(object.getVelX() + object.getMaxSpeed());
}
}
}
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
for(GameObject object : myHandler.objects)
{
if(object.getId() == ID.player)
{
if(key == KeyEvent.VK_W) object.setVelY(object.getVelY() + object.getMaxSpeed());
if(key == KeyEvent.VK_S) object.setVelY(object.getVelY() - object.getMaxSpeed());
if(key == KeyEvent.VK_A) object.setVelX(object.getVelX() + object.getMaxSpeed());
if(key == KeyEvent.VK_D) object.setVelX(object.getVelX() - object.getMaxSpeed());
}
}
}
}
答案 0 :(得分:0)
将速度设置为最大速度或零而不是添加或减去最大速度可能会解决问题。
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
for(GameObject object : myHandler.objects)
{
if(object.getId() == ID.player)
{
if(key == KeyEvent.VK_W) object.setVelY(-object.getMaxSpeed());
if(key == KeyEvent.VK_S) object.setVelY(object.getMaxSpeed());
if(key == KeyEvent.VK_A) object.setVelX(-object.getMaxSpeed());
if(key == KeyEvent.VK_D) object.setVelX(object.getMaxSpeed());
}
}
}
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
for(GameObject object : myHandler.objects)
{
if(object.getId() == ID.player)
{
if(key == KeyEvent.VK_W) object.setVelY(0);
if(key == KeyEvent.VK_S) object.setVelY(0);
if(key == KeyEvent.VK_A) object.setVelX(0);
if(key == KeyEvent.VK_D) object.setVelX(0);
}
}
}
答案 1 :(得分:0)
我已经解决了这个问题,它是在添加额外速度后释放键时发生的,但是在夹子踢入之前发生了。我通过在键输入本身添加速度检查来解决这个问题。
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
for(GameObject object : myHandler.objects)
{
if(object.getId() == ID.player)
{
if(!(object.getVelY() == 8))
{
if(key == KeyEvent.VK_S) object.setVelY(object.getVelY() + object.getMaxSpeed());
}
if(!(object.getVelY() == -8))
{
if(key == KeyEvent.VK_W) object.setVelY(object.getVelY() - object.getMaxSpeed());
}
if(!(object.getVelX() == -8))
{
if(key == KeyEvent.VK_A) object.setVelX(object.getVelX() - object.getMaxSpeed());
}
if(!(object.getVelX() == 8))
{
if(key == KeyEvent.VK_D) object.setVelX(object.getVelX() + object.getMaxSpeed());
}
}
}
}
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
for(GameObject object : myHandler.objects)
{
if(object.getId() == ID.player)
{
if(key == KeyEvent.VK_W) object.setVelY(object.getVelY() + object.getMaxSpeed());
if(key == KeyEvent.VK_S) object.setVelY(object.getVelY() - object.getMaxSpeed());
if(key == KeyEvent.VK_A) object.setVelX(object.getVelX() + object.getMaxSpeed());
if(key == KeyEvent.VK_D) object.setVelX(object.getVelX() - object.getMaxSpeed());
}
}
}