MessageList.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  2. // const GameModule = require("../utils/GameModule");
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. messageItem: cc.Prefab,
  7. /// 最大同时8条信息流
  8. maxCount: 8,
  9. },
  10. // LIFE-CYCLE CALLBACKS:
  11. onLoad () {
  12. this._items = [];
  13. this._itemsData = [];
  14. this.updateInitData(-1);
  15. GameEvent.on(GameNotificationKey.GameUpdateMessageList, this, (updateCount, isTime = false) => {
  16. // this._updateCount =
  17. this.updateInitData(updateCount, isTime);
  18. });
  19. this.schedule(this.timeAction, 1);
  20. },
  21. updateInitData(updateCount, isTime) {
  22. let timeInformations = Global._timeInformations;
  23. let itemDatas = timeInformations.concat(Global._fixInformations);
  24. let originLength = this._itemsData.length;
  25. let upLength = updateCount == -1 ? itemDatas.length : updateCount;
  26. let max = upLength + originLength;
  27. let sub = max - 8;
  28. /// 如果当前的长度已经大于8了
  29. if (sub > 0) {
  30. let timeLength = timeInformations.length;
  31. for (let i = 0; i < sub; i++) {
  32. let index = timeLength + i;
  33. this.hiddenItem(index);
  34. }
  35. max -= sub;
  36. originLength -= sub;
  37. }
  38. for (let i = originLength; i < max; ++i) {
  39. let item = cc.instantiate(this.messageItem);
  40. let itemData = itemDatas[i];
  41. item.getComponent('MessageItem').init(itemData);
  42. this.node.addChild(item);
  43. this._items.push(item);
  44. }
  45. this._itemsData = itemDatas;
  46. if (isTime === true) {
  47. this.reloadAllItemData();
  48. }
  49. },
  50. reloadAllItemData() {
  51. for(let i = 0; i < this._items.length; ++ i) {
  52. let item = this._items[i];
  53. let itemData = this._itemsData[i];
  54. item.getComponent('MessageItem').init(itemData);
  55. }
  56. },
  57. timeAction() {
  58. let hiddenIndexArr = [];
  59. for (let i = 0; i < this._items.length; ++ i) {
  60. let itemNode = this._items[i];
  61. let itemScript = itemNode.getComponent('MessageItem');
  62. itemScript.updateTime();
  63. if (itemScript._messageData.cdTime === 0) {
  64. hiddenIndexArr.push(i);
  65. }
  66. }
  67. if (hiddenIndexArr.length > 0) {
  68. for (let i = 0; i < hiddenIndexArr.length; ++ i) {
  69. this.hiddenItem(hiddenIndexArr[i]);
  70. }
  71. }
  72. },
  73. hiddenItem(index) {
  74. if (index < this._items.length) {
  75. let item = this._items[index];
  76. let fadeAction = cc.fadeTo(0.5, 0); //0.5秒透明度从255降到0
  77. let action = cc.sequence(fadeAction, cc.callFunc(() => {
  78. this._items.splice(index, 1);
  79. item.destroy();
  80. item = null;
  81. this._itemsData.splice(index, 1);
  82. if (index < Global._timeInformations.length) {
  83. Global._timeInformations.splice(index, 1);
  84. } else {
  85. Global._fixInformations.splice(index - Global._timeInformations.length - 1, 1);
  86. }
  87. }));
  88. item.runAction(action);
  89. }
  90. },
  91. start () {
  92. },
  93. // update (dt) {},
  94. });