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 – indicating a game over event in the console


We target the 3-intersection point between the horizontal line and the vertical line.

  1. We have defined three DIV elements there to display the graphics when hitting the J, K, and L keys. We modify the HTML to add a data-line-no attribute to these elements:

    <div id="hit-line-1" data-line-no="1" class="hit-line hide"></div>
    <div id="hit-line-2" data-line-no="2" class="hit-line hide"></div>
    <div id="hit-line-3" data-line-no="3" class="hit-line hide"></div>
  2. We move to JavaScript. We define a new function inside the audiogame object:

    initTouchAndClick: function() {
      var game = this;
      $('.hit-line').bind('mousedown touchstart', function() {
        var line = $(this).data('lineNo') * 1; // parse in int
        game.hitOnLine(line);
        return false;
      });
    
      $('.hit-line').bind('mouseup touchend', function(){
        var line = $(this).data('lineNo') * 1; // parse in int
        $('#hit-line-'+line).removeClass('show...