Game Development Reference
In-Depth Information
Because the buttons vary in width, the local variable
prevButton
is created to store that last button that
was drawn inside of the loop. You do this so you can access its width and properly position the next button in
line. Before being added to
buttonHolder
, a
click
event listener is attached to each button and will trigger the
onAttackButtonSelected
method. Figure
14-13
shows the complete visuals for the
BattlePanel
class.
Figure 14-13.
The complete battle panel in the game
The next two methods are used to enable and disable all buttons in the panel. These methods will be used often
during a battle. For instance, when a user attacks an enemy, all buttons should instantly become inactive. The next time
the wait bar has reached its end, the player can make another move and the available buttons will again become active.
If you recall, when the button is pressed, it will call the
onAttackButtonSelected
method. Listing 14-28 shows
this function.
Listing 14-28.
BattlePanel.js - The Click Event Handler for the Buttons
p.onAttackButtonSelected = function (e) {
if (this.currentAttackButton != null) {
this.currentAttackButton.enableButton();
}
this.currentAttackButton = e.currentTarget;
this.currentAttackButton.selectButton();
var event = new events.BattleButtonEvent(events.ATTACK_BUTTON_SELECTED,
false, false, this.currentAttackButton.type);
this.dispatchEvent(event);
}
Each time a button is clicked in a player's turn, it is stored in the
currentAttackButton
property
.
This is for a few
reasons, but for now it's used to reset it in the case the user decides to click another. Remember, clicking a button will
select it, giving the player the option to then choose their target. If the player selects
fire,
but then decides he just wants to
attack
, he can do so. This would mean that the current button was the fire button, and it should now call its
enableButton
method. The new attack button is then set to
currentAttackButton
and its
selectButton
method is called.
The next thing that happens is new. A custom event is being instantiated and dispatched. You've used plenty
of events so far, but this is the first true
custom
event that has been utilized. What this means is that a class called
BattleButtonEvent
was created that extends
Event
. Doing this allows you to pass custom parameters into the event,
which can then be retrieved by the handler. This class will be built in the “Creating Custom Events” section, but for
now, just notice the last parameter. This is the attack type of the button currently in play, which is string, and will be
evaluated when the attack finally commences on a target back in the game.