A castle can fire bullets to kill enemies. In this task, we add bullets to the game with enemy-contacting detection.
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>
Let's follow the given steps to be able to fire bullets and attack the enemies:
Define the
Bullet
class in thebullet.js
file. It contains the bullet graphics and a defaultdamageDeal
property with a set value. However, theCastle
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...