Book Image

Instant HTML5 2D Platformer

By : Aidan Temple
Book Image

Instant HTML5 2D Platformer

By: Aidan Temple

Overview of this book

Game development has traditionally only been accessible to those with experience in computer science and access to the best or most expensive development tools. However, with the advent of technologies such as HTML5 and the ability to self-publish, web-based games such as those on Facebook and smartphones are becoming more attractive to develop than ever before. Through the use of open technologies such as HTML5, anyone with even a basic understanding of games development can begin to develop video games in their spare time and publish them to the Web or as an application for mobile devices. Instant HTML5 2D Platformer is a practical, hands-on guide that provides you with a number of clear, step-by-step, task-based exercises, which are used to discuss game development and put into practice development techniques through the use of HTML5 and JavaScript. This book looks at the creation of a 2D platform-based game using the HTML5 canvas element. Instant HTML5 2D Platformer introduces you to HTML5 canvas through a number of exercises, which show what the canvas is capable of. The book contains a number of clear, practical, hands-on tasks that incrementally build on the concepts of game creation and result in a 2D HTML5 platform-based game. By undertaking the tasks within this book, you will learn how to develop your own 2D HTML5 game framework that you can use in the creation of your own video games, not just the game developed within this book. Alongside this framework you will learn how to develop and understand 2D animation, game logic, and how to handle user input devices.
Table of Contents (7 chapters)

Animating the player (Must know)


For this task, we will focus on enhancing the visual appeal of the game by introducing and demonstrating the necessary steps required to animate the player sprite. These steps will involve adjusting the game framework to handle the loading and updating animated sprite sheets.

How to do it...

  1. To begin with, we must introduce a new object to the games framework. This object will be responsible for animating the player sprite through means of a sprite sheet.

  2. Create a new object called AnimationManager. With the animation object open, copy the following code in it:

    function AnimationManager() {
    
        this.currentFrame = 0;
        this.frameRate = 0;
        this.frameTime = 0;
        this.frameWidth = 0;
    this.frameHeight = 0;
    
        this.InitAnimationManager = function(texture, x, y, z, frameCount, framesPerSec) {
    
            this.InitDrawableObject(texture, x, y, z);
            this.currentFrame = 0;
            this.frameCount = frameCount;
            this.frameRate = 1 / framesPerSec;
            this.frameTime = this.frameRate;
            this.frameWidth = this.texture.width / this.frameCount;
     this.frameHeight = this.texture.height;
    
            return this;
        }
    
    this.Draw = function(deltaTime, context, deltaX, deltaY) {
    
    var sourceRect = this.frameWidth * this.currentFrame;
    context.drawImage(this.texture, sourceRect, 0,this.frameWidth,  this.frameHeight, this.x - deltaX, this.y - deltaY,  this.frameWidth, this.frameHeight);
    
    this.frameTime -= deltaTime;
    
    if (this.frameTime <= 0)
    {
    this.frameTime = this.frameRate;
    ++this.currentFrame;
    this.currentFrame %= this.frameCount;
    }
    }
    }
    
    AnimationManager.prototype = new DrawableObject;
  3. The next step is to modify the call to initialize the drawing of the player sprite. Inside of the Player object constructor replace the InitDrawableObject call with the following:

    this.InitAnimationManager(player_idle_left, 300, 300, 1, 6, 20);
  4. Similarly replace the prototype property at the bottom of the player object with the following:

    Player.prototype = new AnimationManager;
  5. The final step is to replace the player sprite with the sprite sheet animation, which can be found within the Chapter 1 Task 5 images folder provided with the book. If you run the application you should see an animated player sprite in the top-left corner of the application.

How it works...

The animation manager is an extension of the drawable object we created previously and as a result introduces an additional draw function that is able to output animated sprites. The animation manager begins by declaring a number of variables that relate to the frame rate of the given sprite, the time taken to animate the sprite as well as the width and height of a given frame within a sprite sheet.

Each of these variables is initialized within the InitAnimationManager constructor. The animation manager constructor also takes in a texture, that is, a sprite sheet, a 2D position, and a depth variable used to determine the drawing order of objects.

The draw method within the animation manager is responsible for creating a source rectangle, which is positioned above the first cell in the sprite sheet. For each frame the source rectangle is positioned above the next cell within the sprite sheet. Once the source rectangle reaches the end of the sprite sheet its position is reset to the first cell. By doing this we are selectively picking out and displaying one cell at a time from the sprite sheet. We then loop through each cell at a frame rate, which gives the illusion of animation, in this case 12 frames per second.

In order to display an animated sprite on the canvas, we replaced the call to initialize drawing of the player sprite within the player constructor. Previously, we passed the player texture and position to the DrawableObject constructor. However, now that we are animating the player we also need to pass a frame count and frame rate, that is, the number of cells within the sprite sheet and the rate at which the animation is updated per second. Finally, we use the prototype keyword to extend the functionality of the DrawableObject constructor to the AnimationManager function.