Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 20 ■ Coding gameplay: Set Up gameplay methodS and animated Camera View

Notice that the random number for each game board square attached to each quadrant is generated
in the populateQuadrant() methods to select and set up the image assets used. We’ll also use this random
number result in the setupQ1S1gameplay() method to determine what quadrant texture map image to
display, if a player has clicked that square to select that content for their question. This is because each
square has more than one image.
Since this setupQ1S1gameplay() method is called as a result of a mouse click on your Q1S1 Node object,
the first thing you will need to do is to change the default texture map for game board quadrant 1 to instead
be the texture map, which matches the content shown in game board square 1, which was clicked. There will
be other Java statements added later that set up the question and answer options for the image content, but
let’s start with the visual feedback that the player will get when clicking the game board content.
Since there are currently three different content images that might be populating game board square 1,
you will have three if() constructs that will contain the Java statements relating to each content selection.
The random number generator has already randomly selected one of these three content images in your
populateQuadrantOne() method using the pickS1 variable to store this selection. So, logically we should
use this variable, which you have now made a “global” game variable, to ascertain which quadrant texture
map to set the diffuse21 quadrant texture Image object to, using the Image() object constructor with the
image asset name, resolution, and rendering settings. Then, all you have to do is to set the Shader21 object
to reference this (new) diffuse21 Image object using the setDiffuseMap() method call. This will be done
for each of the three content options, with the gamequad1bird0 through gamequad1bird2 image file name
being the primary code element that will change between three different conditional if() constructs inside of
the setupQ1S1gameplay() method. This makes the copy-and-paste coding work process the logical one to
utilize.
The Java code for your setupQ1S1gameplay() method should look like the following code, as
shown at the top of Figure 20-4 as well as copied and pasted at the bottom of the figure to create a
setupQ1S2gameplay() method:


private void setupQ1S1gameplay() {
if (pickS1 == 0 ) {
diffuse21 = new Image("gamequad1bird0.png", 512, 512, true, true, true);
Shader21.setDiffuseMap(diffuse21);
}
if (pickS1 == 1 ) {
diffuse21 = new Image("gamequad1bird1.png", 512, 512, true, true, true);
Shader21.setDiffuseMap(diffuse21);
}
if (pickS1 == 2 ) {
diffuse21 = new Image("gamequad1bird2.png", 512, 512, true, true, true);
Shader21.setDiffuseMap(diffuse21);
}
}

Free download pdf