const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey; // const GameModule = require("../utils/GameModule"); cc.Class({ extends: cc.Component, properties: { messageItem: cc.Prefab, /// 最大同时8条信息流 maxCount: 8, }, // LIFE-CYCLE CALLBACKS: onLoad () { this._items = []; this._itemsData = []; this.updateInitData(-1); GameEvent.on(GameNotificationKey.GameUpdateMessageList, this, (updateCount, isTime = false) => { // this._updateCount = this.updateInitData(updateCount, isTime); }); this.schedule(this.timeAction, 1); }, updateInitData(updateCount, isTime) { let timeInformations = Global._timeInformations; let itemDatas = timeInformations.concat(Global._fixInformations); let originLength = this._itemsData.length; let upLength = updateCount == -1 ? itemDatas.length : updateCount; let max = upLength + originLength; let sub = max - 8; /// 如果当前的长度已经大于8了 if (sub > 0) { let timeLength = timeInformations.length; for (let i = 0; i < sub; i++) { let index = timeLength + i; this.hiddenItem(index); } max -= sub; originLength -= sub; } for (let i = originLength; i < max; ++i) { let item = cc.instantiate(this.messageItem); let itemData = itemDatas[i]; item.getComponent('MessageItem').init(itemData); this.node.addChild(item); this._items.push(item); } this._itemsData = itemDatas; if (isTime === true) { this.reloadAllItemData(); } }, reloadAllItemData() { for(let i = 0; i < this._items.length; ++ i) { let item = this._items[i]; let itemData = this._itemsData[i]; item.getComponent('MessageItem').init(itemData); } }, timeAction() { let hiddenIndexArr = []; for (let i = 0; i < this._items.length; ++ i) { let itemNode = this._items[i]; let itemScript = itemNode.getComponent('MessageItem'); itemScript.updateTime(); if (itemScript._messageData.cdTime === 0) { hiddenIndexArr.push(i); } } if (hiddenIndexArr.length > 0) { for (let i = 0; i < hiddenIndexArr.length; ++ i) { this.hiddenItem(hiddenIndexArr[i]); } } }, hiddenItem(index) { if (index < this._items.length) { let item = this._items[index]; let fadeAction = cc.fadeTo(0.5, 0); //0.5秒透明度从255降到0 let action = cc.sequence(fadeAction, cc.callFunc(() => { this._items.splice(index, 1); item.destroy(); item = null; this._itemsData.splice(index, 1); if (index < Global._timeInformations.length) { Global._timeInformations.splice(index, 1); } else { Global._fixInformations.splice(index - Global._timeInformations.length - 1, 1); } })); item.runAction(action); } }, start () { }, // update (dt) {}, });