Game Development Reference
In-Depth Information
recall, the button class will take care of the rest visually. The updateHeroInventory and onEnemyAttackedComplete
methods are then called, which will be covered in the “Attacking the Enemies” section. While taking a potion is not
attacking an enemy, the next steps to be taken in the game are handled within that function, so it is called here.
Not You might be wondering why i went through the hassle of creating a custom event to carry the selected
button's type when i could have easily accessed the selected button via the battle panel instance. While this might be
true in this case, i find it cleaner to use custom events whenever possible. though i am using both approaches in this topic,
i encourage you to use custom events if the concept is clear to you, as opposed to digging into an object's properties.
after all, that object might not exist anymore in many situations, so the knowledge of custom events should prove beneficial
in your future applications.
Attacking the Enemies
Finally the enemies can be attacked! Most of the attack code is within the Enemy class itself; the game just needs to
initiate it. Listing 14-35 shows all enemy attacking methods.
Listing 14-35. Game.js - The Game Methods for Attacking Enemies
p.attackEnemy = function (e) {
var enemy = e.currentTarget;
var player = data.PlayerData.player;
var btn = this.battlePanel.currentAttackButton;
btn.updateQuantity(-1);
this.updateHeroInventory(this.attackSelected);
this.disableEnemyTargets();
this.battlePanel.disableButtons();
enemy.takeDamage(this.hero.power, this.attackSelected);
}
p.onEnemyAttackedComplete = function (e) {
this.attackSelected = false;
this.battlePanel.resetPanel();
this.checkLevel();
}
p.onEnemyDestroyed = function (e) {
var i, enemy;
for (i = 0; i < this.enemies.length; i++) {
enemy = this.enemies[i];
if (enemy === e.target) {
this.enemies.splice(i, 1);
break;
}
}
this.enemyHolder.removeChild(e.target);
this.onEnemyAttackedComplete(null);
}
p.updateHeroInventory = function (item) {
this.hero.updateInventory(item, -1);
}
 
 
Search WWH ::




Custom Search