Game Development Reference
In-Depth Information
if ( isDead() ) {
death();
}
}
}
}
}
Notice that the Flash Builder was nice enough to create the constructor that matches
with the one in its parent class.
Also, notice the keyword override for the causeDamage method. Here we are telling
the compiler that we are overriding the behavior of the causeDamage method from
the super class. Omitting the override keyword produces a compiler error.
Creating a new passive monster object would look something like the following:
var pm:PassiveMonster = new PassiveMonster(10, 10, 10, 10, 10);
Note that via inheritance, the objects of our new class responds to all the methods
defined in the super class, such as update, isAlive, isDead, etc.
Now how about that ActiveMonster ?
From our specification, we want it to attack back the last player that attacked, so
we may leave the causeDamage as is without overriding it in the ActiveMonster
subclass. We also want the monster to actively attack when some player (avatar) is
close by. For this we invent a new method called scan. Also, we will invent a chase
method for chasing a player, in case the player tries to run. The implementation is
not shown here, as it would be heavily dependent on the game specifics and more
so because we want to focus on illustrating inheritance rather than an actual game
implementation at this point.
One way to implement this is to update our base Monster class as follows (notice the
bolded code and the two new methods):
public function update():void {
var now:Number = new Date().getMilliseconds();
var delta:Number = (now - m_lastUpdate);
scan(delta); // scan for a new attacker
chase(delta); // chase the attacker
respawn(delta);
heal(delta);
if ( isAlive() ) {
attack(delta);
}
 
Search WWH ::




Custom Search