Game Development Reference
In-Depth Information
Figure 17-36 . Add callAttack, spriteMove and destination variables and an if(callAttack)-else programming structure
The first thing that we'll need to do, inside the .update() method, is add an
if(!callAttack) if-else conditional structure around the if(attackCounter >= attack-
Frequency) structure that we already have in place. We will leave the attack-
Counter = 0; initialization statement inside of this inner loop, and we will add a
spriteMoveR = 700; and a spriteMoveL = -70; initialization statement.
These will place the Enemy sprite off-screen on either side of the Stage.
A callAttack boolean flag allows us to communicate between the .update() and the
.initiateAttack methods, as you can see, in the .update() method, after the attackCounter
(timer) has allowed the player enough time to collect his wits after enemy attacks, this
callAttack variable is set to a true (attack) value. In a more complex version of your
.initiateAttack() method, you will set this callAttack variable to a false (delay attack)
value, starting the attackCounter.
Let's also make a Java code optimization and take the .setTranslateY() method call
that we have twice in the initiateAttack() method, and make just a single method call
(which represents a 100% savings on usage of the Random object's .nextInt() method
call). Once all these programming statements are in place, you can finally set the
 
Search WWH ::




Custom Search