Book Image

HTML5 Game Development Hotshot

By : Seng Hin Mak, Makzan Makzan (Mak Seng Hin)
Book Image

HTML5 Game Development Hotshot

By: Seng Hin Mak, Makzan Makzan (Mak Seng Hin)

Overview of this book

Table of Contents (15 chapters)
HTML5 Game Development HOTSHOT
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Firing bullets and attacking the enemies


A castle can fire bullets to kill enemies. In this task, we add bullets to the game with enemy-contacting detection.

Prepare for lift off

As usual, we put new things into a dedicated file. We should have an empty bullet.js file ready in the project directory:

<script src="scripts/board-objects/bullet.js"></script>

Engage thrusters

Let's follow the given steps to be able to fire bullets and attack the enemies:

  1. Define the Bullet class in the bullet.js file. It contains the bullet graphics and a default damageDeal property with a set value. However, the Castle implementation can override this value. It also moves up at a constant speed:

    // Bullet
    ;(function(game, cjs, lib){
      function Bullet(damageDeal) {
        cjs.Container.call(this); //super
        this.addChild(new lib.Bullet());
    
        this.cache(-25, -25, 50, 50);
    
        this.damageDeal = damageDeal || 1; // default 1
    
        // tick the movement
        this.on('tick', this.tick);
      }
      Bullet.prototype...