Game Development Reference
In-Depth Information
We also use a class datatype called Avatar in the causeDamage method for the
purpose of explaining the monster class inheritance. We will simply assume Avatar
is another class that holds properties required for an avatar and that just like any
other class, it too offers a set of methods to interact with. For example, you will see
that in the attack method, the avatar can respond to isAlive and causeDamage
methods. Although the method names are identical to that of the monster, they are
completely unrelated and have their own implementation and are not listed here.
The update method is special in that it is called from a top level game loop; the game
loop is explained in more detail in the next section.
With just the Monster class, you can imagine the number of different kinds of
monsters you can create. Although they could be different in their strength, healing
rate, etc., how can we leverage the code in this class to create another class of
monster? (No pun intended!)
Let's say we want to create the passive monster from our initial specification and
want to keep attacking the first attacker even if another player attacks until either
the monster or the player dies or runs away.
Closely examine the causeDamageMethod repeated as follows:
public function causeDamange(damage:Number,
avatar:Avatar):void {
// If alive decrease life, check for death
if ( isAlive() ) {
m_avatar = avatar;
m_life -= damage;
if ( isDead() ) {
death();
}
}
}
Notice that the m_attacker is updated every time a player attacks the monster,
which means the monster starts to attack the last player that attacked it. If we want
to remember the first attacker, we need to modify the method to something
like the following:
public function causeDamange(damage:Number,
avatar:Avatar):void {
// If alive decrease life, check for death
if ( isAlive() ) {
if ( m_avatar != null ) {
m_avatar = avatar;
}
 
Search WWH ::




Custom Search