updateCoin.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. coinPrefab: cc.Prefab,
  6. },
  7. // LIFE-CYCLE CALLBACKS:
  8. onLoad() {
  9. this.coinPool = new cc.NodePool();
  10. for (let i = 0; i < 10; i++) {
  11. let coin = cc.instantiate(this.coinPrefab);
  12. this.coinPool.put(coin);
  13. }
  14. GameEvent.on(GameNotificationKey.PlayUpdateCoinAnimation, this, (position) => {
  15. let canvas = cc.find('Canvas');
  16. let canvasPosition = canvas.convertToNodeSpaceAR(position);
  17. if (this.coinPool.size() > 0) {
  18. let coinNode = this.coinPool.get();
  19. canvas.addChild(coinNode);
  20. coinNode.position = canvasPosition;
  21. coinNode.zIndex = 10;
  22. coinNode.getComponent('SpendCoinCtrl').show(() => {
  23. this.coinPool.put(coinNode);
  24. canvas.removeChild(coinNode);
  25. });
  26. }
  27. });
  28. },
  29. onDestroy() {
  30. GameEvent.off(GameNotificationKey.PlayUpdateCoinAnimation, this);
  31. },
  32. start() {
  33. },
  34. // update (dt) {},
  35. });