HTML and CSS Reference
In-Depth Information
Engage thrusters
With the following steps, we create enemies, make them live on the board, and slowly move
them down to the earth:
1. First, we define the base of the Enemy class. Put the following code in the enemy.
js file. A base enemy moves down at a regular speed:
// Enemy moves down and attacks buildings
;(function(game, cjs, lib){
function Enemy(){
cjs.Container.call(this);
// these are default values. They can be overriden by
instances.
this.originalSpeed = 0.5; // speed may change over time. This
one remains constant.
this.deceleration = 0.004;
this.hp = 10;
this.damageDeal = 10;
this.attackSpeed = 100; // smaller means faster
// properties
this.speed = this.originalSpeed;
this.on('tick', this.tick);
}
Enemy.prototype = Object.create(cjs.Container.prototype);
Enemy.prototype.tick = function() {
if (cjs.Ticker.getPaused()) { return; }
// check if speed <0. Min allowed is 0.
if (this.speed < 0) { this.speed = 0;}
this.y += this.speed;
};
game.Enemy = Enemy;
}).call(this, game, createjs, lib);
2. Based on the Enemy class, we can define a new type of enemy. For example,
it is very easy to kill the following new enemy. It moves slower and has very
low health points:
// An easy-to-kill enemy
;(function(game, cjs, lib){
function EnemyDummy(){
 
Search WWH ::




Custom Search