Book Image

HTML5 Game Development by Example: Beginner's Guide

By : Seng Hin Mak
Book Image

HTML5 Game Development by Example: Beginner's Guide

By: Seng Hin Mak

Overview of this book

Table of Contents (18 chapters)
HTML5 Game Development by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
9
Building a Physics Car Game with Box2D and Canvas
Index

Time for action – making a game guide animation


There is a graphics file named guide_sprite.png in the images folder. It is a game guideline graphic that contains each step of the animation.

Let's draw this guide into our game with animations:

  1. Open the untangle.drawing.js JavaScript file in the text editor.

  2. In the untangleGame.loadImages function, add the following code:

    // load the guide sprite image
    untangleGame.guide = new Image();
    untangleGame.guide.onload = function() {
      // setup timer to switch the display frame of the guide sprite
      untangleGame.guideFrame = 0;
      setInterval(untangleGame.guideNextFrame, 500);
    }
    untangleGame.guide.src = "images/guide_sprite.png";
  3. Still in the untangleGame.drawing.js file, we add the following function to move the current frame to the next frame every 500 milliseconds:

    untangleGame.guideNextFrame = function() {
      untangleGame.guideFrame++;
      // there are only 6 frames (0-5) in the guide animation.
      // we loop back the frame number to frame 0 after frame...