Game Development Reference
In-Depth Information
this.totalCoins -= cost;
player.coins = this.totalCoins;
this.updatePurse(cost);
this.evaluatePurse();
}
p.updatePurse = function (cost) {
var xPos = this.coinsTxt.x;
var yPos = this.coinsTxt.y;
this.removeChild(this.coinsTxt);
this.coinsTxt = this.getBitmapTxt('x' + this.totalCoins, xPos, yPos,
.6);
this.addChild(this.coinsTxt);
}
p.evaluatePurse = function () {
var i, btn, cost;
var btns = ['potion', 'fire', 'earth', 'lightning' ];
var len = btns.length;
for (i = 0; i < len; i++) {
cost = this.magicData[btns[i]];
btn = this.getChildByName('btn_' + btns[i]);
if (cost > this.totalCoins) {
btn.disableButton();
}
}
}
window.game.MagicShop = MagicShop;
}());
The player's total coins is referenced from PlayerData and set to the totalCoins property, and the cost of each
item is referenced from GameData and set to magicData . These values will be used when determining if the player
can buy an item or not. They are also used to build the messaging for the shop in addStoreMessaging and addPurse
functions. These messages are built in the same fashion as the level messaging back in the LevelComplete class.
A small, animated sprite of a spinning coin is added next to the coins text for a nice visual effect.
Next, the shop items are created in the addItemButtons method. The attack buttons are reused for the shop, and
their enablement is determined by the cost of each item and the player's total coins. Their quantity message is set by
the current inventory of the player, and their positioning uses the same approach as when added to the BattlePanel
class. The complete level complete screen is shown in full with the magic shop in Figure 14-18 .
Search WWH ::




Custom Search