Reputation: 75
Alright, i made my own little menu but have a small problem. when i use the 'w' and 's' keys only the play button, and highscore button gets selected. But when i use the arrow keys, they skip the highscores button and just alternate between Play and quit.
*I am using libgdx
public class levelSelection implements Screen {
private SpriteBatch spriteBatch;
private Texture playB;
private Texture exitB;
private Texture hScoreB;
private Texture backGround;
MyGdxGame game;
private int selectedButton;
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( selectedButton == 2 && Gdx.input.isKeyPressed(Input.Keys.DOWN) || Gdx.input.isKeyPressed(Input.Keys.S)){
selectedButton = 3;
System.out.println("Quit button is selected");
}
if(selectedButton == 3 && Gdx.input.isKeyPressed(Input.Keys.UP)|| Gdx.input.isKeyPressed(Input.Keys.W)){
selectedButton = 2;
System.out.println("High Scores button is selected");
}
if(selectedButton == 2 && Gdx.input.isKeyPressed(Input.Keys.UP)|| Gdx.input.isKeyPressed(Input.Keys.W)){
selectedButton = 1;
System.out.println("Play button is selected");
}
if(selectedButton == 1 && Gdx.input.isKeyPressed(Input.Keys.DOWN)|| Gdx.input.isKeyPressed(Input.Keys.S)){
selectedButton = 2;
System.out.println("High Scores button is selected");
}
//end navigation between menu buttons
if(selectedButton == 1 && Gdx.input.isKeyPressed(Input.Keys.ENTER)){
game.setScreen(game.GameScreen);
}
if(selectedButton == 3 && Gdx.input.isKeyPressed(Input.Keys.ENTER)){
game.dispose();
}
//Draw text according to selected button
if(selectedButton == 2){
spriteBatch.begin();
font.draw(spriteBatch, "High Score Button is Selected!", 15, 15);
spriteBatch.end();
}
if(selectedButton == 3){
spriteBatch.begin();
font.draw(spriteBatch, "Quit Button is Selected!", 15, 15);
spriteBatch.end();
}
if(selectedButton == 1){
spriteBatch.begin();
font.draw(spriteBatch, "Play Button is Selected!", 15, 15);
spriteBatch.end();
}
}
}
Upvotes: 0
Views: 72
Reputation: 6231
true || false = true
false || true = true
Lets combine this with a selectedButton == 3 ( lets say is false) And we have the first keydown not pressed but the second keydown. Then we do have this situation false && false || true
-> false || true = true
. And this should not happen. It's not selected but the key ifstatemen says true
so it is not correct. (System.out.println(false && false || true);
)
Here is the right solution to it. You always need to check both conidions and concatenate them with the or statement:
if((selectedButton == 2 && Gdx.input.isKeyPressed(Input.Keys.DOWN)) || (selectedButton == 2 && Gdx.input.isKeyPressed(Input.Keys.S))){
selectedButton = 3;
System.out.println("Quit button is selected");
}
This would create the following boolean statemen false || false && false ||true
-> false && true
-> false
.
Just to mention a more sweet solition you can also use the XOR instead of the 2 && concatinated with an ||.
if(selectedButton == 2 && Gdx.input.isKeyPressed(Input.Keys.DOWN) ^ Gdx.input.isKeyPressed(Input.Keys.S)){
selectedButton = 3;
System.out.println("Quit button is selected");
}
Or brace the key or key.
I am sorry if this isnt sematically fully correct. I am not concerned with the boolean order of java.
Upvotes: 2