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

Selecting the pattern

In this task, we allow players to select the pattern from their decks and display the sequence of the selection in the composition view.

Engage thrusters

Perform the following steps to add user interaction to our game:

  1. We allow players to undo their selection, so we need to add an undo button to the index.html file:
    <a href="#" id="undo-button" class="button">Undo</a>
  2. When starting a level in the game.js file, we store the player's selection sequence and register the clicking event by adding the following highlighted code:
    startLevel: function() {
      game.quest = new game.Quest(this.currentLevel);
      game.compositionSeq = [];
      game.composition = new game.Composition();
      game.gameScene.visualize(game.quest);      
      game.gameScene.handleInput();
    },
  3. In the patch.js file, we need to add forEach to the NodeList and HTMLCollection objects using the following code:
    NodeList.prototype.forEach = Array.prototype.forEach;
    HTMLCollection.prototype.forEach = Array.prototype.forEach;
  4. In the composition-view.js file, we need the following methods to display the pattern selection in the composition's DOM element:
    game.compositionView = {
      node: document.getElementById('your-composition'),
      pushPattern: function(patternId) {
        var newChild = document.createElement('div');
        newChild.classList.add('pattern');
        newChild.setAttribute('data-pattern', patternId);
        this.node.appendChild(newChild);
      },
      pullPattern: function() {
        var lastChild = this.node.querySelector('.pattern:last-child');
        if (lastChild) {
          // find the pattern in the deck and make it visible
          var deckNode = document.getElementById('deck');
          var resumePattern = deckNode.querySelector('[data-pattern="' + lastChild.getAttribute('data-pattern') + '"]');
          resumePattern.style.display = 'block';
    
          // remove the current pattern
          this.node.removeChild(lastChild);
        }
      },
      selectPattern: function(pattern) {
        this.pushPattern(pattern);
        game.compositionSeq.push(pattern);
      },
      undo: function() {
        this.pullPattern();
        game.compositionSeq.pop();
      },
    };
  5. Then, we need the mouse event to invoke our selection logic. In the scenes.js file, we add the following clicking event to the gameScene:
    gameScene.handleInput = function() {         
      document.querySelectorAll("#deck .pattern").forEach(function(elm){
        elm.onclick=  function(){
          var pattern = elm.getAttribute('data-pattern');
          elm.style.display = 'none';
          game.compositionView.selectPattern(pattern);
        };
      });
    
      var undoBtn = document.getElementById('undo-button');
      undoBtn.onclick = function(e){
        game.compositionView.undo();
        e.preventDefault();
      };
    };
  6. Let's move to styling. We have a new undo button, so we need the following CSS rules to place it in the right position with the image:
    #undo-button {
      position: absolute;
      right: 70px;
      top: 240px;
      z-index: 999;
      background: url(images/undo_btn.png) no-repeat;
      width: 90px;
      height: 26px;
    }
    #undo-button:hover {background-position: 0 -26px;}
  7. Also, we add mouse-related styling to the pattern's slot:
    .pattern-slot:hover{outline-color: #D68700;}
    .pattern-slot:active {outline-color: #BC7702;}

Objective complete – mini debriefing

The selection is done by the click event on the pattern. Basically, we get the pattern ID from the data- attribute. Once the pattern ID is known, it triggers the following method:

game.compositionView.selectPattern(pattern);

Then, the composition pushes the selection into an array.

Undo the player composition

We trigger the undo logic by listening to the undo button's click event. Then, the undo logic removes the last pattern from the array. At the same time, we find the last pattern element in the composition view and move this element to the pattern deck.

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