Every time I press a key, Right in this case, I need my p1Start value, in this case the p1StartX to increment by 1. This is used to "Draw" a path in a 2D array of type rectangle.
My problem is, it only increments once. How can I fix this?
scene2.setOnKeyPressed(e -> {
int p1StartX = 5;
int p1StartY = 25;
int p2StartX = 60;
int p2StartY = 25;
if (e.getCode() == KeyCode.LEFT) {
p1.setDirection(1);
p1.update();
} else if (e.getCode() == KeyCode.RIGHT) {
p1StartX++;
gameGrid[p1StartY][p1StartX].setFill(p1.getPlayerClr());
System.out.println("Player 1: Right");
}
This happens because p1StartX
is defined inside the scope of your listener.
Move it to a class member (static or not - up to your design...) - and things will be OK
Moreover, I'm pretty sure this goes for all of your defined int
values.
Like so:
//some class definition...
private int p1StartX;
//Use a constructor to init the value of p1StartX.
...
scene2.setOnKeyPressed(e -> {
....
p1StartX++;
}
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.