Java 为什么我的球员在跳跃后没有下来?
我目前正在尝试编写我的第一个GUI游戏,玩家可以从一个障碍跳到另一个障碍。当按下空格键时,玩家应该做一个简单的跳跃(实际上没有在x轴上移动),但当按下空格键时,玩家只会向上跳,不会向下跳 以下是我的代码中的变量:Java 为什么我的球员在跳跃后没有下来?,java,game-physics,Java,Game Physics,我目前正在尝试编写我的第一个GUI游戏,玩家可以从一个障碍跳到另一个障碍。当按下空格键时,玩家应该做一个简单的跳跃(实际上没有在x轴上移动),但当按下空格键时,玩家只会向上跳,不会向下跳 以下是我的代码中的变量: private int playerY = 415, playerX = 100, score=0, maxHeight = 350; private float speed=3, jumpStrength, weight=1; 以下是我让玩家跳跃的代码: public vo
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;
}
}
按下公共无效键(按键事件e){
int key=e.getKeyCode();
if(key==KeyEvent.VK_空间){
跳跃强度=24;
跳跃强度+=重量;
嬉戏——跳跃的力量;
}
如果(playerY>=最大高度){
playerY=最大高度;
}
}
public void keyTyped(KeyEvent e){}
公共无效密钥已释放(密钥事件e){
int key=e.getKeyCode();
if(key==KeyEvent.VK_空间){
跳跃强度=24;
跳跃强度+=重量;
嬉戏——跳跃的力量;
}
如果(PlayerY)有与此代码相关联的物理引擎吗,例如,你有重力函数来降低你的玩家?!如果没有,那么你需要实现一些类似“PlayerY+=someValue”的代码为了让它们降下来。你知道当他跳跃时你正在减少跳跃强度吗?
playerY-=jumpStrength;
你可能应该在将跳跃强度应用于游戏之前降低跳跃强度。