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 – adding touch support


Carry out the following steps to make our game work in a tablet with touch inputs:

  1. In index.html file, we add the following touch controls before the end of the #game-container:

    <div id="left-button" class="touch-control"></div>
    <div id="right-button" class="touch-control"></div>
    <div id="restart-button" class="touch-control">Restart</div>
  2. We can also add a <meta> tag inside the <head> tag to control the viewport to fit the game into the iPad's 1024 px width.

    <meta name="viewport" content="width=device-width, initial-scale=0.78, minimum-scale=0.78, maximum-scale=0.78">
  3. For these controls, we add some basic styles to position them. To do this, append the following code to the cargame.css file:

    .touch-control {
      position: absolute;
    }
    #left-button {
      top: 0;
      left: 0;
      width: 50%;
      height: 100%;
    }
    #right-button {
      top: 0;
      right: 0;
      width: 50%;
      height: 100%;
    }
    #restart-button {
      top: 0;
      left...