Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
HTML5 Game Development Hotshot

You're reading from   HTML5 Game Development Hotshot Build interactive games with HTML, DOM, and the CreateJS Game library.

Arrow left icon
Product type Paperback
Published in Jul 2014
Publisher
ISBN-13 9781849695466
Length 366 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Makzan Makzan (Mak Seng Hin) Makzan Makzan (Mak Seng Hin)
Author Profile Icon Makzan Makzan (Mak Seng Hin)
Makzan Makzan (Mak Seng Hin)
Seng Hin Mak Seng Hin Mak
Author Profile Icon Seng Hin Mak
Seng Hin Mak
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Building a CSS Quest Game FREE CHAPTER 2. Card Battle! 3. Space Runner 4. Multiply Defense 5. Building an Isometric City Game 6. Space Defenders 7. A Ball-shooting Machine with Physics Engine 8. Creating a Sushi Shop Game with Device Scaling Index

Adding a countdown timer to the game

In this task, we are going to give a final touch to the game by adding a countdown timer. With the timer, the players are not only challenged to select the correct sequence but also restricted in terms of time in order to make the game more exciting for the player.

Prepare for lift off

We will need a new file for the timer. Let's create a new empty file and name it timer.js. With the new file, we also need to import it in the index.html file, which is done using the following code:

<script src='js/timer.js'></script>

The timer would be a horizontal bar at the top of the game. Its width decreases when you count down the time.

Prepare for lift off

Engage thrusters

Let's count down the game with the following steps:

  1. In the index.html file, we need to add a div method for the timer. Add the following code to the HTML inside #game:
    <div id="timer"></div>
  2. Let's move to the game.js file to add the timer control to the game flow:
    gameWin: function() {
      ...
      game.timer.stop();
    },
    gameOver: function() {
      ...
      game.timer.stop();
    },
    startLevel: function() {
      ...
      game.timer.restart();
    },
  3. We remove the following code from the init method in order to prevent the timer from starting during the menu scene:
    var init = function() {
      game.flow.startLevel();   <<<<< Delete this line.
    }
  4. The timer is something that can be reused in other games, so it is worth creating a dedicated file named timer.js. Let's put the following timer code into the newly created timer.js file:
    (function(){
      var game = this.colorQuestGame = this.colorQuestGame || {};
    
      game.timer = {
        interval: undefined,
        countFrom: 60, // second
        count: this.countFrom,
        progressView: document.getElementById('timer'),
        restart: function() {
          if (this.interval) {
            clearInterval(this.interval);
          }
          this.count = this.countFrom;
          this.interval = setInterval((this.tick).bind(this), 1000);
        },
        stop: function() {        
          clearInterval(this.interval);       
        },
        tick: function() {
          this.count -= 1;
          if (this.count<= 0) {
            this.count = 0;
            clearInterval(this.interval);
            game.flow.gameOver();
           }        
          // update the view
          var progress = this.count / this.countFrom * 100;    
          this.progressView.style.width = progress + "%";
        }
      }
    })();
  5. Now, it is time for styling. Let's append the following CSS rules to the game.css file:
    #timer {
      position: absolute;
      top: 0;
      left: 0;
      height: 30px;
      width: 100%;
      background: #7ADAF6 url(images/timer_symbol.png) no-  repeat 5px 50%;  
      border-bottom: 1px solid #4F8EA1;
      transition: all .3s ease-out;
    }

Objective complete – mini debriefing

A simple way to count down is to use the setInterval or setTimeout methods. However, for games, we usually want a global timer to act as the world clock. This way, all the elements will listen for the tick of the timer. When the world clock pauses, all game elements stop, when it resumes, all resume. That's why we have a separate timer object.

The timer has its own interval managed. All we need to do is start and stop the timer and it will run the setInterval and clearInterval methods for us.

By default, the tick function checks whether the time is up and also updates the visual timer DOM node with a width that reflects the countdown.

You have been reading a chapter from
HTML5 Game Development Hotshot
Published in: Jul 2014
Publisher:
ISBN-13: 9781849695466
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image