互联网的伟大人民,您好!我决定使用 libGDX 来创建一个简单的内存游戏,以便在 Java 方面做得更好。我已经自己解决并测试了很多东西,但我遇到了一个问题。不能。数字。出。
我已经创建了一个完整的 if 语句集群来创建一个菜单,该菜单具有可以通过箭头键选择的按钮(我不知道如何使它们成为可点击的按钮 :P),它似乎工作正常,除了它会跳过其中一个按钮,即我的高分按钮。我只会发布 levelSelection Screen 类,因为那是我确定问题所在的类。
levelSelection 屏幕类:
package com.me.mygdxgame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class levelSelection implements Screen {
private SpriteBatch spriteBatch;
private Texture playB;
private Texture exitB;
private Texture hScoreB;
private Texture backGround;
MyGdxGame game;
private boolean playButton;
private boolean quitButton;
private boolean highScoresButton;
BitmapFont font;
public levelSelection(MyGdxGame game) {
this.game = game;
}
public void render(float delta) {
Gdx.gl.glClearColor( 0f, 0f, 0f, 1f );
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
spriteBatch.draw(backGround, 0, 0);
spriteBatch.draw(playB, 250, 450);
spriteBatch.draw(exitB, 250, 350);
spriteBatch.draw(hScoreB, 250, 400);
spriteBatch.end();
//Start Navigation Between menu buttons
if(playButton == true && quitButton == false && highScoresButton == false && Gdx.input.isKeyPressed(Input.Keys.DOWN)){
playButton = false;
quitButton = false;
highScoresButton = true;
System.out.println("HighScores button is selected");
}
if(highScoresButton == true && playButton == false && quitButton == false && Gdx.input.isKeyPressed(Input.Keys.DOWN)){
highScoresButton = false;
playButton = false;
quitButton = true;
System.out.println("quit button is selected");
}
if(quitButton == true && playButton == false && highScoresButton == false && Gdx.input.isKeyPressed(Input.Keys.UP)){
quitButton = false;
playButton = false;
highScoresButton = true;
System.out.println("HighScores button is selected");
}
if(highScoresButton == true && quitButton == false && playButton == false && Gdx.input.isKeyPressed(Input.Keys.UP)){
quitButton = false;
highScoresButton = false;
playButton = true;
System.out.println("Play button is selected");
}
//end navigation between menu buttons
if(playButton == true && Gdx.input.isKeyPressed(Input.Keys.ENTER)){
game.setScreen(game.GameScreen);
}
if(quitButton == true && Gdx.input.isKeyPressed(Input.Keys.ENTER)){
game.dispose();
}
//Draw text according to selected button
if(highScoresButton == true){
spriteBatch.begin();
font.draw(spriteBatch, "High Score Button is Selected!", 15, 15);
spriteBatch.end();
}
if(quitButton == true){
spriteBatch.begin();
font.draw(spriteBatch, "Quit Button is Selected!", 15, 15);
spriteBatch.end();
}
if(playButton == true){
spriteBatch.begin();
font.draw(spriteBatch, "Play Button is Selected!", 15, 15);
spriteBatch.end();
}
}
我认为问题在于游戏正在高速运行,并且出于某种原因,当我按下按钮(向上或向下)时,它不止一次这样做。
最佳答案
问题:
它通常以大约 60 fps 的速度运行。所以显然很难让它只发生一次。
解决方案:
对于一次击键,您可以只允许一次转换。
为了实现这一目标,
- 存储最后按下的键。
- 如果当前按下的键与存储的键相同,则什么也不做。
- 在每次按钮焦点转换后更新它的值。
- 如果不再按下该键,则将其值设置为 null(如果条件 :D 再按下一次)
注意事项:
- 不要为每个按钮使用 boolean 值。它使条件非常冗长。相反,使用像
int selectedButtonIndex = 0;
这样的整数并每次更新它的值。 - 这不是在 libgdx 中实现菜单的标准方法。您正在尝试从头开始实现所有内容。但是当涉及到 ui 时,该框架已经提供了许多更高级别的功能。尝试在互联网上搜索 scene2d ui。你会找到很多资源。 (您将在那里获得可点击的按钮。:D)
祝你好运。
关于java - Libgdx(输入)按键似乎跳过了一些东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21593084/