// Learn cc.Class: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html // Learn Attribute: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html var Module = require('Module'); const RoomState = { Invaild: -1, Lock: 0, UnLock: 1, Update: 2, Full: 3 }; cc.Class({ extends: cc.Component, properties: { roomSprite: { default: null, type: cc.Sprite }, animationImgNode: { default: null, type: cc.Node }, roomBuySprites: { default: [], type: cc.SpriteFrame }, buyButton: { default: null, type: cc.Button }, priceLabel: { default: null, type: cc.Label }, buyTipPrefab: { default: null, type: cc.Prefab }, levelLabel: { default: null, type: cc.Label }, earnMoneyLabel: { default: null, type: cc.Label }, }, init: function(config, totalMoney) { this.config = config; this.node.setPosition(config.x, config.y); this.node.width = config.width; this.node.height = config.height; let price = config.price[config.lv]; if (totalMoney >= price) { this.setState(RoomState.Update); } else { this.setState(RoomState.Lock); } this.priceLabel.string = price.toString(); this.levelLabel.string = "LV." + config.lv.toString(); // 从本地资源中读取SpriteFrame cc.loader.loadRes("room/" + config.img, cc.SpriteFrame, (error, spriteFrame)=>{ if (error) { console.log(error); } else { this.roomSprite.spriteFrame = spriteFrame; this.animationImgNode.getComponent(cc.Sprite).spriteFrame = spriteFrame; } }); }, // LIFE-CYCLE CALLBACKS: onLoad () { this.config = null; this.setState(RoomState.Invaild); this.setSchedule(); }, setSchedule: function() { this.schedule(function () { if (this.isEarnMoney()) { let price = this.config.speed[this.config.lv]; this.earnMoneyLabel.string = "$" + price.toString(); let moveAction = cc.moveBy(0.5, cc.p(0, 200)); // let sqawn = cc.spawn([moveAction, cc.fadeOut(0.5)]); let callBack = cc.callFunc(function () { this.earnMoneyLabel.node.setPosition(cc.p(0, 0)); // this.earnMoneyLabel.node.opacity = 255; Global.GameEvent.fire("earn_money", price); }, this); let sequence = cc.sequence(moveAction, callBack); this.earnMoneyLabel.node.runAction(sequence); } }, 1.0); }, clickBuyButton: function() { if (Module.game.getComponent('game').getBuyTipNode() !== null) { return; } if (this.config !== undefined && this.state === RoomState.Update) { if (this.config.lv > 0) { let buyTipPrefab = cc.instantiate(this.buyTipPrefab); buyTipPrefab.getComponent('BuyTip').init(this, this.config, this.roomSprite.spriteFrame); buyTipPrefab.setScale(0, 0); buyTipPrefab.parent = Module.game; let action = cc.scaleTo(0.2, 1.0, 1.0).easing(cc.easeOut(0.95)); buyTipPrefab.runAction(action); Module.game.getComponent('game').setBuyTipNode(buyTipPrefab); } else { this.updateAnimation(); Global.GameEvent.fire("room_update", this); } } }, isEarnMoney: function() { if (this.config.lv === 0) { return false; } return true; }, getRoomConfig: function() { return this.config; }, unlockUpdate: function() { this.setState(RoomState.Update); }, lockUpdate: function() { this.setState(RoomState.Lock); }, full: function() { this.setState(RoomState.Full); }, setState: function(state) { if (this.state === state) { return; } switch (state) { case RoomState.Lock: this.buyButton.getComponent(cc.Button).normalSprite = this.roomBuySprites[0]; break; case RoomState.Update: this.buyButton.getComponent(cc.Button).normalSprite = this.roomBuySprites[1]; break; case RoomState.Full: this.buyButton.node.active = false; default: break; } this.state = state; }, updateAnimation: function () { let scaleAction = cc.scaleTo(0.35, 1.5, 1.5); let spawn = cc.spawn([scaleAction, cc.fadeOut(0.35)]); let callBack = cc.callFunc(function () { this.animationImgNode.opacity = 255; this.animationImgNode.setScale(1.0, 1.0); }, this); let sequence = cc.sequence(spawn, callBack); this.animationImgNode.runAction(sequence); }, // update (dt) {}, });