[英]Why is my player not coming down after jumping?
我目前正在嘗試編寫我的第一個 GUI 游戲,其中玩家從一個障礙跳到另一個障礙。 當按下空格鍵時,玩家應該做一個簡單的跳躍(實際上並沒有在 x 軸上移動),但是當按下空格鍵時,玩家只會上升而不會下降。
這是我的代碼中的變量:
private int playerY = 415, playerX = 100, score=0, maxHeight = 350;
private float speed=3, jumpStrength, weight=1;
這是我讓玩家跳躍的代碼:
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE && playerY>=maxHeight) {
jumpStrength = 24;
playerY -= jumpStrength;
jumpStrength -= weight;
}
if (playerY>=maxHeight){
playerY = maxHeight;
}
}
public void keyTyped( KeyEvent e ) {}
public void keyReleased( KeyEvent e ){}
有沒有人知道為什么它不起作用以及我如何解決它?
我不知道你的其余代碼看起來如何或你的游戲如何工作,但這是我的一個想法。
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE) {
jumpStrength = 24;
jumpStrength += weight;
playerY -= jumpStrength;
}
if (playerY>=maxHeight){
playerY = maxHeight;
}
}
public void keyTyped( KeyEvent e ) {}
public void keyReleased( KeyEvent e ){
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE) {
jumpStrength = 24;
jumpStrength += weight;
playerY -= jumpStrength;
}
if (playerY<=0){
playerY = 0;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.