Game Development Reference
In-Depth Information
Figure 9-10 . Add a switch-case statement inside of the public void handle() method setting Boolean direction vari-
ables
Now we have the basic key-pressed event handling structure, which we'll be
adding to a bit later, let's have NetBeans turn this Java 7 code into a Java 8 lambda ex-
pression for us! After that, we can create a key-released event handling structure by
using a block copy and paste operation, turning the .setOnKeyPressed() to
.setOnKeyReleased(), and the true values to false values. Programming shortcuts are
almost as cool as having NetBeans write code for us!
Converting the KeyEvent Handling Structure: Using a
Java 8 Lambda Expression
Next let's have NetBeans recode our EventHandler<KeyEvent> code structure as a
lambda expression, which will simplify it significantly, reducing it from a three-deep
nested code block to one that is nested only two deep, and from eleven lines of code to
only eight. These lambda expressions are really elegant for writing tight code, and they
are designed for multi-threaded environments, so whenever possible their usage could
 
Search WWH ::




Custom Search