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)

Creating the player (Must know)


With a basic game framework in place, we can start by developing an object that will handle the player and any behaviors they may have. This will involve expanding upon the game framework to handle the loading of a sprite, which will be used to represent the player within the game.

How to do it...

  1. We must first begin by creating a new object called Player. Once created, insert the following code into our player object. This object is responsible for initializing and drawing the player on the canvas as well as stating the players position on the canvas.

    function Player() {
        this.InitPlayer = function() {
    
            this.InitDrawableObject(player_idle, 0, 0, 0);
            return this;
        }
    }
    
    Player.prototype = new DrawableObject;
  2. With our player object completed, we then need to load the sprite texture into our application. In order to do this, we need to edit the Main.js object we created previously, open Main.js and before the Onload function we will declare a new image object, which will be used to hold our sprite texture.

    var player_idle = new Image();
    player_idle.src = "idle_left.png";
  3. The final step is to initialize our player object by calling the player constructor and passing the player sprite to it.

    this.player = new Player().InitPlayer(player_idle);
  4. If we were to open the application's HTML file in any modern browser we would see the player sprite drawn in the top-left corner of the canvas.

How it works...

When creating a new game object, in this case the player object, we must first create a new image object within the Main object and then assign an external path to the object by setting the source property. In return this results in a texture being loaded into our application that is the player sprite texture. This texture is then passed to the player object and is assigned a position and draw order on the canvas; this is done by means of the drawable object script. In return, the constructor within Main is passed the player sprite and initializes a new player object, which then draws the player sprite to the canvas.