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

Showing different quests

In this task, we are going to show how to advance to the next level when the player's composition matches that of the quest compositions in the current level.

Engage thrusters

This time, we need quest-level data in the quest.js file to be accessible from another file, so we need to attach the questLevels and questData methods to the game scope rather than the original local scope:

  1. In the quest.js file, we change the questLevels declaration from var questLevels to game.questLevels.
  2. We apply the same to questData:
    // from
    var questData = questLevels[level]; 
    // to
    var questData = game.questLevels[level];
  3. In the scenes.js file, we display the level with the following function:
    gameScene.updateLevelInfo = function(level) {
      document.getElementById('stage').textContent = "Stage "+ level;
    };
  4. At last, we modify the game flow in the game.js file to count the level:
    game.flow = {
      currentLevel: -1,
      maxLevel: game.questLevels.length - 1,
      startOver: function() {
        ...
        this.currentLevel = -1;
      },
      nextLevel: function() {
        this.currentLevel+=1;
        if (this.currentLevel>= this.maxLevel) this.currentLevel = this.maxLevel;
    
        game.gameScene.updateLevelInfo(this.currentLevel+1); 
        // when displaying level, we start from 1 instead of 0, so +1 here.
        ...
      },
      ...
    }

Objective complete – mini debriefing

The level up is done by counting the current level and selecting the level from the questLevels array. The nextLevel method is used to increase the currentLevel counter. Moreover, this method is called once startButton is clicked from the menu. Therefore, it will increase the currentLevel counter at the beginning. This is why we set the initial value of currentLevel to -1. This will ensure that it selects the first level once the game starts that is at index 0 of the level array. We reset currentLevel in the startOver method once the game is over. On the other hand, we display the current level at the top of the game inside the #stage element. Therefore, we also update that wording every time the level is up.

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