cretor 小游戏获得物品弹窗

Myth丶恋晨 2022-10-17 00:49 212阅读 0赞

因我们需求是一次最多显示5个 分批显示,所以我先写了一个小模板

两种方式显示 一种是每次显示5个 另一种是 每次显示一个 慢慢向上飘

  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. mainNode: cc.Node,
  5. itemNode: cc.Node,
  6. btn: cc.Button,
  7. QieHuanBtn: cc.Button,
  8. currLabel: cc.Label,
  9. },
  10. // use this for initialization
  11. onLoad() {
  12. window.log = function () {
  13. this.apply(window, arguments);
  14. }.bind(console.log);
  15. this.itemPool = new cc.NodePool();
  16. this.updateTimer = 0;//更新时间累加
  17. this.updateInterval = 0.1;//更新间隔
  18. Object.defineProperty(this, "getGainType", {
  19. get: function () {
  20. return this.value
  21. },
  22. set: function (newValue) {
  23. this.value = newValue;
  24. this.currLabel.string = `当前:${this.getGainType}`;
  25. let layout = this.mainNode.getComponent(cc.Layout);
  26. switch (newValue) {
  27. case 1:
  28. this.updateInterval = 1.4;
  29. layout.enabled = true;
  30. break;
  31. case 2:
  32. this.updateInterval = 0.3;
  33. layout.enabled = false;
  34. break;
  35. }
  36. }
  37. })
  38. this.getGainType = 2;//1:一次显示5个 2: 一次显示1个
  39. this.mainNode.removeAllChildren();
  40. this.itemArr = [1, 2, 3, 4, 5, 6];
  41. this.btn.node.on('click', () => {
  42. this.addItem(~~(Math.random() * 10))
  43. })
  44. this.QieHuanBtn.node.on('click', () => {
  45. this.getGainType = this.getGainType == 1 ? 2 : 1;
  46. })
  47. },
  48. addItem(num) {
  49. log('添加物品:', num)
  50. //解析操作
  51. while (num > 0) {
  52. this.itemArr.push(~~(Math.random() * 100))
  53. num--;
  54. }
  55. },
  56. //对象池
  57. getItemNode() {
  58. let node = null;
  59. if (this.itemPool.size() > 0) {
  60. node = this.itemPool.get();
  61. } else {
  62. node = cc.instantiate(this.itemNode);
  63. }
  64. node.y = 0;
  65. return node;
  66. },
  67. //回收复用
  68. removeItemNode(node) {
  69. this.itemPool.put(node)
  70. },
  71. //增加item
  72. addItemNode(item) {
  73. let node = this.getItemNode();
  74. this.setItemNode(node, item)
  75. if(this.getGainType == 1){
  76. node.setScale(1);
  77. }else{
  78. node.setScale(0.2);
  79. }
  80. node.opacity = 0;
  81. // this.mainNode.insertChild(node, 0);
  82. this.mainNode.addChild(node);
  83. this.tweenNode(node);
  84. },
  85. //设置节点信息
  86. setItemNode(node, item) {
  87. let label = node.getChildByName('label');
  88. label.getComponent(cc.Label).string = `物品${item} X ${item}`;
  89. },
  90. //移动规则
  91. tweenNode(node) {
  92. if (this.getGainType == 1) {
  93. cc.tween(node)
  94. .to(0.2, { opacity: 255 })
  95. .delay(0.6)
  96. .to(0.5, { opacity: 0 })
  97. .call(() => {
  98. this.removeItemNode(node);
  99. })
  100. .start()
  101. } else {
  102. cc.tween(node)
  103. .to(0.1, { opacity: 255, scale: 1 })
  104. .to(0.5, { position: cc.v2(0, 100) })
  105. .to(0.5, { position: cc.v2(0, 200), opacity: 0, scale: 0.2 })
  106. .call(() => {
  107. this.removeItemNode(node)
  108. })
  109. .start()
  110. }
  111. },
  112. update(dt) {
  113. this.updateTimer += dt;
  114. if (this.updateTimer < this.updateInterval) return; // 节流
  115. this.updateTimer = 0;
  116. this._updateItem(dt);
  117. },
  118. _updateItem() {
  119. if (this.itemArr.length) {
  120. if (this.getGainType == 1) {
  121. this.updateInterval = 1.4;
  122. let arrLen = this.itemArr.length;
  123. this.itemArr.forEach((item, index) => {
  124. if (index < 5) {
  125. this.addItemNode(item)
  126. }
  127. })
  128. if (arrLen > 5) {
  129. this.itemArr.splice(0, 5);
  130. } else {
  131. this.itemArr.splice(0, arrLen);
  132. }
  133. log('splice', this.itemArr)
  134. } else {
  135. this.addItemNode(this.itemArr[0])
  136. this.itemArr.splice(0, 1);
  137. }
  138. } else {
  139. // if (this.isStart) {
  140. // let node = this.mainNode.getChildByName('itemNode')
  141. // if (!node) {
  142. // this.close();
  143. // }
  144. // }
  145. }
  146. },
  147. close() {
  148. this.destroyNode();
  149. },
  150. destroyNode() {
  151. log('销毁')
  152. // this.itemPool.clear();
  153. // this.node.removeFromParent();
  154. // this.node.destroy();
  155. },
  156. onDestroy() {
  157. },
  158. });

代码不多,但都是自己的思路,如果有类似的需求可以借鉴。

随便写的 所以样式很丑

测试项目下载

发表评论

表情:
评论列表 (有 0 条评论,212人围观)

还没有评论,来说两句吧...

相关阅读