Game Development Reference
In-Depth Information
fully randomized Enemy attack. The code that we'll be putting together during the re-
mainder of this chapter is the equivalent of turning the computer processor into our
game player's opponent. Let's write code!
The Foundation of an Enemy Class Attack: The .up-
date() Method
Let's start by writing the foundation of our iBeagle Enemy auto-attack engine. The first
thing that we want to do is to “throttle” the 60 FPS pulse engine, and make sure that at-
tacks only happen every four seconds. This is done using the attackCounter and attack-
Frequency variables inside of an if() structure, which counts between the two variables.
If the attackCounter reaches 250 , it is reset to 0 and an initiateAttack() method is
called. Otherwise (else) the attackCounter is incremented using +=1. You could also
use attackCounter++ to accomplish this. The code for the basic conditional if() struc-
ture, which can be seen highlighted in the middle of Figure 17-30 , should look like the
following Java method:
public void update() {
if( attackCounter >= attackFrequency ) {
attackCounter=0;
initiateAttack();
} else {
attackCounter+=1;
}
}
 
Search WWH ::




Custom Search