为什么我的播放器跳起来后不掉下来?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么我的播放器跳起来后不掉下来?相关的知识,希望对你有一定的参考价值。
我目前正在尝试编写我的第一个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;
}
}
以上是关于为什么我的播放器跳起来后不掉下来?的主要内容,如果未能解决你的问题,请参考以下文章