0
我正在做一個正交瓷磚地圖Java遊戲,當我的船移動到x和y邊界按方向鍵它停止移動(如預期),但如果我繼續按下我的角色離開屏幕。Java遊戲 - 關鍵按
這裏是代碼我使用:
@Override
public boolean keyDown(int keycode) {
ship = world.getShip();
switch(keycode){
case Keys.W:
if (ship.getPosition().y<28.5f){
ship.getVelocity().y = 1;}
else {
ship.getVelocity().y = 0;
}
break;
case Keys.S:
if (ship.getPosition().y1){
ship.getVelocity().y = -1;}
else {
ship.getVelocity().y = 0;
}
break;
case Keys.A:
if (ship.getPosition().x0)
ship.getVelocity().x = -1.5f;
else{
ship.getVelocity().x = 0;
}
break;
case Keys.D:
if (ship.getPosition().x<39){
ship.getVelocity().x = 1;
}
else{
ship.getVelocity().x = 0;
}
break;
default:
break;
}
return true;
}
@Override
public boolean keyUp(int keycode) {
ship = world.getShip();
switch(keycode){
case Keys.W:
if(ship.getVelocity().y == 1)
ship.getVelocity().y = 0;
break;
case Keys.S:
if(ship.getVelocity().y == -1)
ship.getVelocity().y = 0;
break;
case Keys.A:
if(ship.getVelocity().x == -1.5f)
ship.getVelocity().x = 0;
break;
case Keys.D:
if(ship.getVelocity().x == 1)
ship.getVelocity().x = 0;
break;
default:
break;
}
return true;
}
我應該怎麼做,以保持在屏幕上我的船,如果一個關鍵是保持推?
請使用代碼格式設置代碼,輸入/輸出和結構化文檔,如HTML或XML。爲此,請選擇樣本並單擊郵件發佈/編輯表單上方的「{}」按鈕。 –