I need to switch between two numbers
by pressing "q"
.
First press:
number = 1;
Second press:
number = 2;
Third press:
number = 1;
Fourth press:
number = 2;
etc
Got method
and variable
:
private static int counter = 0;
private int switchNumbers() {
if (Gdx.input.isKeyJustPressed(Input.Keys.Q)){
counter = 1;
}
if (counter == 1 && Gdx.input.isKeyJustPressed(Input.Keys.Q)){
counter = 2;
}
if (counter == 2 && Gdx.input.isKeyJustPressed(Input.Keys.Q)){
counter = 1;
}
return counter;
}
variable counter
always equals 1.
How can achieve it (switching numbers)
?
you can check what is the current value of counter if its 1 then put 2 else put 1:
by a ternary opeator
:
private static int counter = 0;
private int switchNumbers() {
if (Gdx.input.isKeyJustPressed(Input.Keys.Q)) {
counter = counter == 1 ? 2 : 1;
}
return counter;
}
or simple using if-else
:
private int switchNumbers() {
if (Gdx.input.isKeyJustPressed(Input.Keys.Q)) {
if(counter == 1) {
counter = 2;
} else {
counter = 1;
}
}
return counter;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.