const DWTool = require("../utils/DWTool"); const AlertManager = require('../utils/AlertManager'); const {GameNotificationKey} = require('../utils/GameEnum'); const ArtistTrainApi = require('../net/ArtistTrainApi'); cc.Class({ extends: cc.Component, properties: { content: cc.Node, scrollView: cc.ScrollView, countDownLabel: cc.Label, refreshLabel: cc.Label, tipLabel: cc.Label, refreshBtn: cc.Button, countDown: { get: function() { if (!this._countDown) { this._countDown = 0; } return this._countDown; }, set: function (value) { this._countDown = value; this.countDownLabel.string = DWTool.calculateTime(this._countDown); this._preCountDown = this._countDown; } }, isTraining: { get: function() { if (this._isTraining === undefined) { this._isTraining = false; } return this._isTraining; }, set: function (value) { this._isTraining = value; if (this._isTraining) { this.refreshLabel.node.active = false; this.tipLabel.node.active = false; this.refreshBtn.node.active = false; } else { this.refreshLabel.node.active = true; this.tipLabel.node.active = true; this.refreshBtn.node.active = true; } } }, }, init(targetUid) { this.targetUid = targetUid; }, // LIFE-CYCLE CALLBACKS: onLoad () { this.countDown = 600; this._currentTime = 0; this.getNetworkData(); Global.GameEvent.on(GameNotificationKey.BeginArtistTrain, this, (missionId) => { this.clickStartTrain(missionId); }); Global.GameEvent.on(GameNotificationKey.ArtistTrainCompletion, this, (missionId) => { ArtistTrainApi.missionGain(this.targetUid, missionId, (responseData) => { console.log("任务已完成!"); Global.GameEvent.fire(GameNotificationKey.RefreshUserInformation); }, (err, msg) => { console.log(msg); }); this.close(); }); }, onDestroy() { Global.GameEvent.off(GameNotificationKey.BeginArtistTrain, this); Global.GameEvent.off(GameNotificationKey.ArtistTrainCompletion, this); }, start() { this.content.y = -cc.view.getVisibleSize().height; this.scheduleOnce(() => { let s = cc.sequence(cc.moveTo(0.2, 0, -40).easing(cc.easeCubicActionOut()), cc.moveBy(0.05, 0, -20)); this.content.runAction(s); }, 0.1); }, getNetworkData() { ArtistTrainApi.missionGetTrainings(this.targetUid, (responseData) => { this.missionData = responseData.list; if (responseData.list.length != undefined && responseData.list.length > 0) { for (let item of responseData.list) { if (item.status === 1 || item.status === 2) { this.isTraining = true; } else { this.isTraining = false; } } } else { // 设置为true可以将倒计时隐藏起来 this.isTraining = true; } this.layout(); }, (err, msg) => { console.log("error: " + msg); }); }, clickStartTrain(missionId) { ArtistTrainApi.missionTrains(this.targetUid, missionId, (responseData) => { this.missionData = responseData.list; this.isTraining = true; this.layout(); }, (err, msg) => { console.log("error: " + msg); }); }, layout() { for (let child of this.scrollView.content.children) { child.destroy(); } DWTool.loadResPrefab("./prefabs/artist_train_item") .then((result) => { for (let i = 0; i < this.missionData.length; i++) { let item = cc.instantiate(result); item.getComponent('ArtistTrainItem').init(this.targetUid, this.missionData[i], this.node.zIndex); this.scrollView.content.addChild(item); } }); }, close() { this.node.destroy(); }, diamondRefresh() { AlertManager.showRechargeAlert(this.node.zIndex); }, update(dt) { if (this.isTraining) { return; } if (Math.floor(this._currentTime) === this.countDown) { this._currentTime = 0; this.getNetworkData(); } else { this._currentTime += dt; let resultCountDown = this.countDown - Math.floor(this._currentTime); if (this._preCountDown !== resultCountDown) { this.countDownLabel.string = DWTool.calculateTime(resultCountDown); this._preCountDown = resultCountDown; } } } });