HTML and CSS Reference
In-Depth Information
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-contacing detecion.
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 ire bullets and atack 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
implementaion 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 = Object.create(cjs.Container.prototype);
Bullet.prototype.tick = function(){
if (cjs.Ticker.getPaused()) { return; }
// movement
this.y -= 1.5;
};
game.Bullet = Bullet;
}).call(this, game, createjs, lib);
 
Search WWH ::




Custom Search