Game Development Reference
In-Depth Information
I'm sure you are anxious to get into this custom event, as well as the battle button class, but first let's wrap up the
BattlePanel
class with the final methods (see Listing 14-29).
Listing 14-29.
BattlePanel.js - The Update Functions for the BattlePanel Class
p.update = function () {
if (!this.waitingToAttack) {
this.updateWaitBar();
}
this.updateStats();
}
p.updateWaitBar = function () {
var scale = this.waitBar.scaleX + (.001 * this.SPEED);
if (scale > 1) {
scale = 1;
}
this.waitBar.scaleX = scale;
if (scale == 1) {
this.waitingToAttack = true;
this.enableButtons();
}
}
p.updateStats = function () {
this.hpTxt.text = this.hero.HP + '_' + this.hero.maxHP;
}
p.resetPanel = function () {
this.waitingToAttack = false;
this.waitBar.scaleX = 0;
this.disableButtons();
this.mouseEnabled = true;
this.currentAttackButton = null;
}
The next three methods are used to update the battle panel. The
update
method is continuously called from the
game during the game loop. It has two primary purposes: to update the wait bar, and to update the hero's current
hit points. If the
waitingToAttack
property is set to false, which means the wait bar should still be in progress, the
updateWaitBar
method is called. This will simply update the scale of the bar. When the scale of the bar reaches 1, the
available action buttons are then enabled and
waitingToAttack
is set to true.
The
updateStats
method is constantly called and updates the hit point text by using the instance of the hero
that was passed in from the game. Lastly,
resetPanel
is used to start the whole process over again after a turn as been
successfully executed.
Before stepping back to create the
BattleButton
and
BattleButtonEvent
classes, take a look at the
BattlePanel
class in its entirety, shown in Listing 14-30.