This page is optimized for mobile devices, if you would prefer the desktop version just click here

1.14 Game 2302-0140: our first 3d game program  (Page 8/27)

Servicing the Start button

The code in Listing 2 is executed when the user clicks the Start button. All of this code is also very similar to code that I have explained inprevious modules, so further explanation beyond the embedded comments should not be necessary. However, I will emphasize two statements in Listing 2 .

The first statement that I will emphasize (about two-thirds of the way down) enables the Attack button. The second statement (at the end) instantiates a new object of the Thread class named Animate and causes the run method of the animation thread to be executed. I will explain the animation thread later.

Code that services the Attack button

The code in Listing 3 is executed when the player clicks the Attack button after it is enabled by the code in Listing 2 .

Listing 3 . Code that services the Attack button.
if(e.getActionCommand().equals("Attack")&&animate){ attack = true;predatorVec = predator.getDisplacementVector(preyCenter).scale(1.0);//Disable the Attack button. It will be enabled // again when the attack is finished.attackButton.setEnabled(false); }//end if

Listing 3 begins by setting the value of the variable named attack to true. As you will see later, this causes the predator to attack the preyfish in the animation loop.

Control the speed and direction of the predator attack

Then Listing 3 calls the getDisplacementVector method and the scale method of the game-math library to set the value of the variable named predatorVec to a direction that will be used to point the predator toward the center of the prey-fish cluster. In Listing 3 , the scale factor is 1.0, so the application of the scale factor does nothing. However, you can modify thisvalue to control the speed of the predator during the upcoming attack. If you increase the scale factor, the predator will move faster. If you decrease thescale factor, the predator will move more slowly.

Temporarily disable the Attack button

Finally, Listing 3 temporarily disables the Attack button so that it will have no effect if the player clicks it again during the attack. You couldalso remove this statement, slow down the attack, and experiment with multiple clicks on the Attack button during the course of an attack to see what happens.

Code that services the Stop button

The code in Listing 4 is executed when the player clicks the Stop button.

Listing 4 . Code that services the Stop button.
if(e.getActionCommand().equals("Stop")&&animate){//This will cause the run method to terminate and // stop the animation. It will also clear the// attack flag so that the next time the animation // is started, the predator won't be in the// attack mode. animate = false;attack = false; }//end if}//end actionPerformed

No explanation beyond the embedded comment should be required for this code.

Beginning of the Animate class and the run method

When the Start button is clicked, the code in Listing 2 instantiates a new object of the Thread class named Animate and causes the run method belonging to that object to be executed. Listing 5 shows the beginning of the Animate class and its run method.

<< Chapter < Page Page > Chapter >>

Read also:

OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.