Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 17 ■ i3D Game Square SeleCtion: uSinG the piCkreSult ClaSS with 3D moDelS


Now that you’ve created and loaded a Node object named picked with the Node object in your
BoardGame, which has been clicked with the mouse (or touchscreen, which also generates mouse events)
by the user, we need to add the conditional processing logic (artificial intelligence) to tell the game how to
operate. The first thing that you need to do is filter out all clicks that are not on 3D Node objects, which is
done by using the if (picked != null) construct, which says if the picked Node object is not empty, then
proceed. The next nested if() statement looks for a spinner Node object to be the same as (== or equivalent
to) the picked Node object. If this equates to a true value, the rotGameBoard Animation object is triggered
by using the .play() method call, spinning the gameBoard Group Node. If you use the Run ➤ Project work
process and test this code, it works perfectly, although you have to wait until the code for the last chapter
finishes (we will be fixing that next, as we change Animation objects to be MouseEvent triggered).
The entire completed Java 9 structure is only eight lines of code; this will grow as we build the game
logic. The completed Java method body’s code is shown here and is highlighted in yellow and blue in
Figure 17-4:


private void createSceneProcessing() {
scene = new Scene(root, 1280, 640, true, SceneAntialiasing.BALANCED);
scene.setFill(Color.BLACK);
scene.setCamera(camera);
scene.setOnMouseClicked(event->{
Node picked = event.getPickResult().getIntersectedNode();
if (picked != null) {
if (picked == spinner) {
rotGameBoard.play();
}
}
});
}


Figure 17-3. Configure event handling as a lambda expression, create a Node named picked, and get an
intersected Node

Free download pdf