HTML and CSS Reference
In-Depth Information
if (target !== undefined) {
enemy.speed -= enemy.deceleration;
enemy.startAttack(target);
} else {
enemy.stopAttack();
}
}
4. In the construcion funcion of the
Enemy
class, we add the following properies
about the atack:
// attack
this.isAttacking = false;
this.attackingTarget = undefined;
this.attackingSmoke = new lib.Explode();
this.attackingSmoke.y = 50;
5. We also add the
tick
funcion that controls the atack on the target:
Enemy.prototype.tick = function() {
...
// existing code
// attack the building every once a while
if (cjs.Ticker.getTicks() % this.attackSpeed === 0) {
if (this.isAttacking && this.attackingTarget) {
this.attackingTarget.damage(this.damageDeal);
}
}
};
6. We define the following
startAttack
method to atack the building that's in front
of the enemy. Whenever we launch an atack, we reassign the target building to
make sure it is the correct one:
// Attack and damage
Enemy.prototype.startAttack = function(targetBuilding) {
if (!this.isAttacking) {
this.isAttacking = true;
this.addChild(this.attackingSmoke);
}
this.attackingTarget = targetBuilding; //reassign target
};