[英]Make a player jump to specific height in LIBGDX
I want to implement a jump in LIBGDX on clicking buttons in LIBGDX. 我想在单击LIBGDX中的按钮时实现LIBGDX中的跳转。 Everything goes fine but on clicking the button continuously the player keeps on going high .If I didn't release my button its going beyond the screen height . 一切正常,但连续单击该按钮,播放器将继续升高。如果我不松开按钮,它将超出屏幕高度。 So, I want the player to jump only certain height on each button click and though the button is clicked continuously I want the player to go only certain height and not go beyond the screen. 因此,我希望播放器在每次单击按钮时仅跳到一定高度,尽管连续单击该按钮,但我希望播放器仅移到一定高度而不要超出屏幕。 Here is my code, 这是我的代码,
for (int i = 0; i < 5; i++) {
if (buttonup.isPressed()) {
if (Gdx.input.isTouched(i)) {
gamehero.heroBody.setLinearVelocity(0, 1f);
Gdx.app.log("up is presd", "");
}
}
if (buttonright.isPressed()) {
if (Gdx.input.isTouched(i)) {
gamehero.heroBody.setLinearVelocity(1.2f, 0);
}
}
}
On executing this code, the game works just like FLAPPY BIRD. 执行此代码后,游戏就像FLAPPY BIRD一样工作。 But I want to reach only certain height . 但是我只想达到一定的高度。 I have tried various methods and referred this stack answer too but nothing helped I tired limiting the height using this.gamehero.heroBody.getLinearVelocity().y >= -1 too but didn't work . 我尝试了各种方法,也引用了该堆栈的答案,但没有任何帮助让我厌倦了使用this.gamehero.heroBody.getLinearVelocity()。y> = -1来限制高度,但是没有用。 Please help . 请帮忙 。 Thanks in advance 提前致谢
Implement InputListener
for a class and call: 为一个类实现InputListener
并调用:
Gdx.input.setInputProcessor(thatClass);
Then override keyUp
in thatClass
like this: 然后像这样重写thatClass
keyUp
:
@Override
public boolean keyUp (int keycode) {
if (keycode == Keys.SPACE) {
//code that make your bird jump
}
}
This will make your jump code runs only once. 这将使您的跳转代码仅运行一次。 If you don't wan't double jumps add a boolean member alreadyJumping
and set it true when the jump starts and set it false when the jump is ended. 如果您不希望两次跳转,请添加一个布尔成员alreadyJumping
跳转,并在跳转开始时将其设置为true,在跳转结束时将其设置为false。
@Override
public boolean keyUp (int keycode) {
if (keycode == Keys.SPACE && !alreadyJumping) {
alreadyJumping = true;
//code that make your bird jump
}
}
If you want your jump to slow down and you character fall decrease you velocity based on deltaTime
when you render your game. 如果您希望跳跃变慢并且角色下降,则在渲染游戏时会根据deltaTime
降低速度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.