你好再次哦哦强大的互联网人!我决定使用libGDX创建一个简单的内存游戏,以便在java中变得更好。我已经解决了,并且已经自己测试了很多东西,但是我遇到了一个问题,我只是。不能。数字。进行。
我创建了一个完整的if语句集群来创建一个菜单,其中包含可以通过箭头键选择的按钮(我无法弄清楚如何使它们成为可点击的按钮:P)它似乎工作正常它会跳过其中一个按钮,这是我的高分按钮。我只会发布levelSelection Screen类,因为那是我确定问题所在的那个。
levelSelection Screen类:
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();
}
}
我认为问题在于游戏是以高速运行的,出于某种原因,当我点击按钮(向上或向下)时,它不止一次。
答案 0 :(得分:3)
通常以大约60 fps的速度运行。所以很明显很难让它只发生一次。
对于一次击键,您只能进行一次击球。
要实现这一目标,
备注:强>
int selectedButtonIndex = 0;
这样的整数,并且每次都更新它的值。