HTML and CSS Reference
In-Depth Information
this.y = (context.canvas.height - maxHeight - this.texture.
height);
this.isOnGround = true;
this.position = 0;
}
else if(this.isOnGround) {
this.isOnGround = false;
this.position = (Math.PI / 2);
}
How it works...
We start off by declaring a number of variables that are used to relect the player's behavior
when jumping. These variables include the maximum jump height, time taken to jump, the
speed and direction of the jump, as well as a Boolean variable that checks whether or not the
player is on the ground.
The next step we took was to determine whether the user had pressed the Space bar key, the
W key, or the up arrow key. If the player has pressed either of these keys and is on the ground
then the player begins to jump. Depending on the keys pressed during the jump, the player
will either jump straight up and return to its initial position or will jump and follow the path of a
sine wave.
When the player reaches the peak of the wave then they will begin to decelerate at a speed of
his/her initial jump speed multiplied by the terminal velocity value previously declared. This
can be seen in the next recipe which employs a series of checks to determine whether the
player is not on the ground, if so then we check where on the path of the sine wave the player
is. The player's speed and position are then updated to relect the player's position in the air.
Finally, we then employ a number of statements that are used to check if the player has
collided with the terrain. Each of these collision detection statements check whether the
left-hand side or right-hand side of the player has collided with the terrain. If the player has
collided with the terrain, we change the IsOnGround Boolean variable to true . However if
the player is not on the ground and was previously on the ground then they are falling and
their speed and position is adjusted accordingly until grounded.
Creating enemies (Must know)
In this section, we will outline the steps taken to implement a number of animated enemy
sprites that the player must attempt to avoid in order to stay alive. We will also look at the
steps taken to implement a simplistic form of artiicial intelligence known as patrolling. By
utilizing this technique, we can give life to each of the enemies by allowing them to move
freely around the level. This will involve the implementation of an Enemy object as well as
adjusting the Level and Main objects to handle the newly created Enemy objects.
 
Search WWH ::




Custom Search