Game Development Reference
In-Depth Information
In the constructor of the PatrollingEnemy class, you load the main animation for the patrolling
enemy character (an angry-looking flame, as shown in Figure 27-2 ). Initially, you set a positive
velocity so the enemy starts walking to the right. You also initialize another member variable called
_waitTime that keeps track of how long the enemy has been waiting on one of the edges of the
platform it's walking on:
this._waitTime = 0;
this.velocity.x = 120;
this.loadAnimation(sprites.flame, "default", true);
this.playAnimation("default");
Figure 27-2. A few patrolling enemies
In the update method, you have to distinguish between two cases: the enemy is walking or waiting.
You can distinguish between these states by looking at the _waitTime variable. If this variable
contains a positive value, the enemy is waiting. If the variable contains a value of zero or less, the
enemy is walking. When the enemy is waiting, you don't have to do much. Just as you did in the
Rocket class, you subtract the elapsed game time from the _waitTime variable. If the wait time has
reached zero, you need to turn the character around. Here is the code to do that:
if (this._waitTime > 0) {
this._waitTime -= delta;
if (this._waitTime <= 0)
this.turnAround();
}
 
Search WWH ::




Custom Search