O'Reilly logo

HTML5 Game Development HOTSHOT by Makzan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required