Game Development Reference
In-Depth Information
....................
r..W...........X....
...--..W.......--...
....W.--........W..R
...--..........--...
r..W......W....W....
...--....--....--...
....W...........W...
...--........W.--...
r..W........--.W....
...--..........--...
....W...........W..R
...--..........--...
.1..................
######..####..######
Many, many, many, many, many rockets...
An 'r' character means that the rocket should fly from left to right, an 'R' character
means that it should fly from right to left.
28.2.1 Creating and Resetting the Rocket
We are going to create a
Rocket
class that represents this particular kind of enemy.
We will inherit from the
AnimatedGameObject
class, since the rocket will be animated.
In the constructor, we initialize the
Rocket
object. We need to load the rocket ani-
mation and play it, and then we need to check if the animation should be mirrored.
Since the animation has the rocket moving to the right, we need to mirror it if the
rocket moves to the left. We also store the starting position of the rocket, so we can
place it back at that position when it moves out of the screen. This is the complete
constructor:
public
Rocket(
bool
moveToLeft, Vector2 startPosition)
{
this
.LoadAnimation("Sprites/Rocket/spr_rocket@3", "default",
false
, 0.5f);
this
.PlayAnimation("default");
this
.Mirror = moveToLeft;
this
.startPosition = startPosition;
Reset();
}
The last instruction in the constructor is a call to the
Reset
method. In this method,
we set the current position of the rocket to the starting position, we set the
Visible
property to
false
(so the rocket is initially invisible), and we set its velocity to zero.
We also use the random number generator to calculate a random time (in seconds)
after which the rocket should appear and start moving. We store this time in a mem-
ber variable called
spawnTime
. The reason we put these instructions in a separate
Reset
method is that we are going to call this method later on as well after the rocket
has flown out of the screen.