Game Development Reference
In-Depth Information
Listing 14-13. Enemy.js - The Methods For Attacking the Enemy
p.takeDamage = function (power, attackType) {
var damage = power - this.data.defense;
this.playAttackedAnimation();
switch (attackType) {
case 'fire':
damage += this.getFireDamage();
break;
case 'earth':
damage += this.getEarthDamage();
break;
case 'lightning':
damage += this.getLightningDamage();
break;
default:
damage += 0;
break;
}
damage = damage > 0 ? damage : 0;
this.healthBar.updateHP(damage);
}
p.playAttackedAnimation = function () {
var event;
var hit = this.enemySprite.clone();
hit.gotoAndStop(this.data.frame + '_hit');
this.addChild(hit);
createjs.Tween.get(hit)
.to({alpha:.3}, 100, createjs.Ease.bounceInOut)
.to({alpha:.6}, 100, createjs.Ease.bounceInOut)
.to({alpha:.2}, 200, createjs.Ease.bounceInOut)
.to({alpha:.3}, 100, createjs.Ease.bounceInOut)
.to({alpha:.6}, 100, createjs.Ease.bounceInOut)
.to({alpha:.2}, 200, createjs.Ease.bounceInOut)
.call(function (hit) {
this.removeChild(hit);
this.removeChild(this.magicSprite);
this.checkHealth();
}, [hit], this);
}
The total damage on the enemy is factored by a few things. One is the power of the hero that is attacking it, and
another is the defense value of the enemy. The last factor is the type of attack given to the enemy. The hero's power
and attack type are both passed into the takeDamage method from the game. Before further calculations are taken
on damage using the attack type, an animation is first created on the enemy in the playAttackedAnimation method.
This simply clones the enemy sprite and loads the red tinted frame of the same enemy from the sprite sheet. This
red sprite clone is flickered to indicate being attacked, then removed when the tween is complete. You'll notice that
magicSprite is also removed. This sprite is added if a magic attack was used and is used to add extra effects over
the attacked enemy sprite. One of three functions might get called, based on the attack used, and is determined in a
switch statement.
Search WWH ::




Custom Search