Game Development Reference
In-Depth Information
Listing 14-34. Game.js - Handling the Clicks on Battle Action Buttons
p.onAttackButtonSelected = function (e) {
if (e.attackType == 'potion') {
this.giveHeroPotion();
}
else {
if (!this.attackSelected) {
this.enableEnemyTargets();
}
this.attackSelected = e.attackType;
}
}
p.giveHeroPotion = function () {
var btn = this.battlePanel.currentAttackButton;
btn.updateQuantity(-1);
this.hero.HP += 20;
if (this.hero.HP > this.hero.maxHP) {
this.hero.HP = this.hero.maxHP;
}
this.updateHeroInventory('potion');
this.onEnemyAttackedComplete();
}
p.enableEnemyTargets = function () {
var i, enemy;
var len = this.enemyHolder.getNumChildren();
for (i = 0; i < len; i++) {
enemy = this.enemyHolder.getChildAt(i);
enemy.enableTarget();
}
}
p.disableEnemyTargets = function () {
var i, enemy;
var len = this.enemyHolder.getNumChildren();
for (i = 0; i < len; i++) {
enemy = this.enemyHolder.getChildAt(i);
enemy.disableTarget();
}
}
First off, if the type of button that was pressed is a potion, then the game should immediately fire the
giveHeroPotion method. Since the target of a potion button will always be the hero, the action is triggered as soon as it
is pressed. If it is another type of action, one that will need an enemy target to commence, then it should show the target
animations on each enemy by calling the enableEnemyTargets method. The disableEnemyTargets method is also
declared here and does exactly what you would expect. The game holds an attackSelected property that will be used
later after selecting an enemy target. This property is set accordingly by accessing the attackType value from the event.
this.attackSelected = e.attackType;
When taking a potion, the hero gets 20 extra hit points, but cannot exceed the max hit point value. Before doing
this, the button needs to be updated to show the new potion quantity. You can access this button via the instance of
the battle panel, battlePanel , and by calling on the button's update method by passing in a value of -1. If you
Search WWH ::




Custom Search