Game Development Reference
In-Depth Information
Listing 14-27. BattlePanel.js - Adding the Battle Action Buttons
p.addButtons = function () {
var i, btn, btnWidth, prevButton;
var btns = ['attack', 'potion', 'fire', 'earth', 'lightning' ];
var player = data.PlayerData.player;
var xPos = 70;
var yPos = 140;
var btnSpacing = 5;
var len = btns.length;
this.buttonHolder = new createjs.Container();
for (i = 0; i < len; i++) {
btn = new game.BattleButton(btns[i], player[btns[i]]);
btnWidth = btn.getBounds().width;
if (prevButton != null) {
btn.x = ((prevButton.x + (prevButton.getBounds().width / 2)) +
btnWidth / 2) + btnSpacing;
}
else {
btn.x = xPos;
}
btn.y = yPos;
btn.on('click', this.onAttackButtonSelected, this);
this.buttonHolder.addChild(btn);
prevButton = btn;
}
this.addChild(this.buttonHolder);
}
p.disableButtons = function () {
var i, btn;
var len = this.buttonHolder.getNumChildren();
for (i = 0; i < len; i++) {
btn = this.buttonHolder.getChildAt(i);
btn.disableButton();
}
}
p.enableButtons = function () {
var i, btn;
var len = this.buttonHolder.getNumChildren();
for (i = 0; i < len; i++) {
btn = this.buttonHolder.getChildAt(i);
if (btn.quantity > 0 || btn.quantity < 0) {
btn.enableButton();
}
}
}
A local array is built to dictate what buttons and in what order they should be laid out in the panel. A loop runs
through this array while making instances of the BattleButton class. These button objects take two parameters: a
string value indicating the type of action it should represent, and the quantity of it the player currently has. If you
recall, the player has a permanent value of -1 for its attack value. This tells the button that it should not use quantity
to determine its enablement and that it should be available forever. This will be covered more when you build this
button class in the next section, “Creating the Battle Action Buttons.”
Search WWH ::




Custom Search