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 – removing missed melody notes


We will store some game play statistics and use them to adjust the melody volume. We will continue with our JavaScript file:

  1. First, add the following variables in the variable declaration region:

    var audiogame = {
      totalSuccessCount: 0,
    
      // storing the success count of last 5 results.
      successCount: 5,
    
      // existing code goes here.
    
    };
  2. We want to not only remove a dot but also keep track of the result when we hit it by using a keyboard. Add the following code inside the hitOnLine function:

    // check if hit a music note dot
    for(var i in audiogame.dots) {
       if (lineNo === audiogame.dots[i].line && Math.abs(audiogame.dots[i].distance) < 20) {
          // remove the hit dot from the dots array
          audiogame.dots.splice(i, 1);
    
          // increase the success count
          audiogame.successCount+=1;
    
          // keep only 5 success count max.
          audiogame.successCount = Math.min(5, audiogame.successCount);
    
          // increase the total success...