Game Development Reference
In-Depth Information
public void Jump( float speed = 1100)
{
velocity.Y =
speed;
}
So, the effect of calling the Jump method without providing any parameter value
is that the y velocity is set to a value of
1100. This is a sort of randomly chosen
number. By choosing a bigger number, the character can jump higher. A lower num-
ber means that our character has to go to the gym more often, or quit smoking. We
chose this value, so that the character can jump high enough to reach the tiles, but
not too high so that the game becomes too easy (then the character could just jump
to the end of the level).
There is a minor problem with this approach: we always allow the player to jump,
no matter what the current situation of the character is. So, if the player is currently
jumping or falling down a cliff, we allow the player to jump his/her way back to
safety. This is not really what we want. We want the player only to jump when
he/she is standing on the ground. This is something that we can detect by looking at
collisions between the player and wall or platform tiles (which are the only tiles that
the player can stand on). Let us assume, for now, that our yet to be written collision
detection algorithm will take care of this, and it will keep track of whether the player
is on the ground or not by using a member variable:
protected bool isOnTheGround;
If this member variable is true , we know that the player is standing on the ground.
We can now change our initial if -instruction so that it only allows a player to jump
from the ground and not from the air:
if ((inputHelper.KeyPressed(Keys.Space) || inputHelper.KeyPressed(Keys.Up))
&& isOnTheGround)
Jump();
27.5 . . . and Falling
The only place where we are currently changing the y velocity is in the HandleInput
method, when the player wants to jump. Since the y velocity will indefinitely keep
the value of
1100, the character will simply move up in the air, outside of the
screen, out of the planet's atmosphere into outer space. Since we are not making
a game about bombs in space, we will have to do something about this. What we
forgot to add to the game world is gravity .
We follow a very simple approach to simulate the effect of gravity on the charac-
ter's velocity. We simply add a small value to the velocity in the y direction in each
update step:
Search WWH ::




Custom Search