Game Development Reference
In-Depth Information
this
.visible =
false
;
GameEnvironment.AssetManager.PlaySound("Sounds/snd_watercollected");
}
The
this
.visible
expression in the condition of the
if
instruction is very important. If
we left that out, the 'water collected' sound would be played every time there was
a collision, even after the drop has been made invisible, and this is not what we
want. Later on, we can check if the level is completed by checking the visibility of
each water drop. If all water drops are invisible, then we know that the player has
collected all of them.
29.3 Ice Blocks
Another type of interaction that we would like to add to the game is special behavior
when the player is walking over ice. When the player moves over ice, we want the
character to continue sliding, and not stop moving when the player release the arrow
key. This means we have to do two things:
•
extend the
HandleInput
method to deal with moving over ice;
•
calculate whether the player is standing on ice or not.
We will keep track of whether the player is standing on ice or not in a member
variable
walkingOnIce
in the
Player
class. Let us assume for now that this variable is
updated somewhere else, and let us first have a look at extending the
HandleInput
method. The first thing we want to do is increase the walking speed when the player
is walking on ice. We can do that as follows:
float
walkingSpeed = 400;
if
(walkingOnIce)
walkingSpeed
∗
=1.5f;
Then, we handle the actual player input. If the player is pressing the left or right
arrow key, we set the appropriate
x
velocity:
if
(inputHelper.IsKeyDown(Keys.Left))
velocity.X =
−
walkingSpeed;
else if
(inputHelper.IsKeyDown(Keys.Right))
velocity.X = walkingSpeed;
We then add another alternative to this
if
instruction that sets the
x
velocity to zero.
We could simply do this:
else
velocity.X = 0f;