Game Development Reference
In-Depth Information
Let us see how we would implement a real-world monster class. The example
monster described here will have the following properties.
Different monsters can have a different attack power or cause more damage to
the player when in battle. Game designers would typically place monsters with a
higher attack power at higher levels within the game. In this example, we will keep
it simple—each monster will have a specific attack power, but in real-world games,
things can get quite complex. For example, the damage caused may depend on the
type of monster and the player type in a battle, whether a monster is a magical kind
that can hurt a warrior more than a wizard class avatar. Further, it could also depend
on the defensive items that the avatar wears or possesses, and other factors.
We will also give the monster the ability to heal itself over time. A variety of monster
objects can be created with different values. For example, a higher level monster
could heal itself quickly, making the player really fight hard to defeat it, or it might
take multiple players to slay it.
Finally, we will also give the monster a respawn capability, meaning that it could
come back to life a certain time after the monster had died. Again, the rate at which
they respawn gives an opportunity to create a variety of monster objects.
Now, for all the different types of monsters, would we make a subclass for each? We
could have a thousand different varieties and a subclass for each! Nah! What we do
is let the monster object accept these values during their creation via the constructors.
The values are then stored inside the class that will never change. Thus each monster
could be different based on what values were passed in during its instantiation.
So first, let's examine the base monster class and then illustrate how we can create
passive and active monsters by subclassing from the parent monster class.
Here is a complete listing for the monster class. There are comments embedded in
the code that you are encouraged to read and an added explanation follows
the listing.
package world
{
public class Monster
{
/**
* Stores the amount of damage the monster
* can cause an avatar.
*/
protected var m_attackPower:Number;
/**
 
Search WWH ::




Custom Search