Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 10 ■ User InterfaCe DesIgn InteraCtIvIty: event hanDlIng anD ImagIng effeCts


case LEFT: left = false; break;
case RIGHT: right = false; break;
}
}
});


One of the interesting things that using lambda expressions does by “implicitly” declaring and using
classes, such as the EventHandler class in the instances in this chapter, is that it reduces the number of
import statements in the top of your class code. This is because if a class is not specifically used (its name
written) in your code, the import statement for that class does not have to be in place at the top of your
code with the other import statements. You’ll also notice that your code-collapsing plus or minus icons in
the left margin of NetBeans 9 will disappear if you convert into the lambda expression. This is because a
lambda expression is a basic Java code statement, and not a construct or structure such as an inner class or a
method, which is what it was before you converted it to a lambda expression.
Now that you have taken a look at how KeyEvent handling structures would be put in place, let’s take
a look at how easy it is to add an alternate key mapping to the ASDW keys that are often used in gameplay.
This is done by adding in a few more case statements for the A, S, D, and W characters on the keyboard and
setting these to the UP, DOWN, LEFT, and RIGHT boolean equivalents (up, down, left, and right variables)
that we have set up already.
This will allow users to use the A and D characters with their left hand and the UP and DOWN arrows
with their right hand for easier gameplay, for instance. Later if you wanted to add more features to the
gameplay using your game controller and its support for the KeyCode class GAME_A, GAME_B, GAME_C,
and GAME_D constants, all that you will have to do to add these new features into your game would be to
add another four boolean variables (a, b, c, and d) to the up, down, left, and right variables at the top of the
class and add in another four case statements.
These four W (UP), S (DOWN), A (LEFT), and D (RIGHT) case statements, once added to the switch
statement, would make your KeyEvent object and its event-handling Java code look like the following 15
lines of Java code:


scene.setOnKeyPressed(new EventHandler() {
@Override
public void handle(KeyEvent event) {
switch (event.getCode()) {
case UP: up = true; break;
case DOWN: down = true; break;
case LEFT: left = true; break;
case RIGHT: right = true; break;
case W: up = true; break;
case S: down = true; break;
case A: left = true; break;
case D: right = true; break;
}
}
});


As you can see, now the user can use either set of keys, or both sets of keys at the same time, to control
the gameplay. Now do the same thing to the .setOnKeyReleased() event-handling structure using a
copy-and-paste work process and change the value to false. The .setOnKeyReleased() event-handling Java
code will look like the following:

Free download pdf