CoinGetEffectLayer.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { instantiate, Node, tween, v3, Vec3, _decorator } from 'cc';
  2. import { BaseLayer } from '../../common/BaseLayer';
  3. import { cocosUtil } from '../../utils/cocosUtil';
  4. import { constants } from '../data/constants';
  5. import { audioManager } from '../manager/audioManager';
  6. import { playerModel } from '../model/playerModel';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('CoinGetEffectLayer')
  9. export class CoinGetEffectLayer extends BaseLayer {
  10. coinLayer: Node;
  11. coinNode: Node;
  12. posStart: Vec3;
  13. onLoad() {
  14. super.onLoad();
  15. this.coinLayer = this.getNodeByPath("coinLayer");
  16. this.coinNode = this.getNodeByPath("coinLayer/coin");
  17. this.posStart = this.obj.posStart;
  18. this.initUI();
  19. }
  20. initUI() {
  21. let pos = cocosUtil.convertToNodeSpace(this.coinNode, this.posStart);
  22. let posEnd = cocosUtil.convertToNodeSpace(this.coinNode, playerModel.instance.coinPosEnd);
  23. let num = 10 + Math.floor(Math.random() * 10);
  24. let count = 0;
  25. for (let i = 0; i < num; i++) {
  26. let coinNode = this.coinNode;
  27. if (i >= 1) {
  28. coinNode = instantiate(this.coinNode);
  29. coinNode.parent = this.coinLayer;
  30. }
  31. let posX = pos.x + Math.random() * 200 - 100;
  32. let posY = pos.y + Math.random() * 200 - 100;
  33. coinNode.setPosition(posX, posY);
  34. coinNode.setScale(0, 0, 1);
  35. let time = 0.5 + Math.random() * 0.5;
  36. tween(coinNode).to(0.2, {
  37. scale: v3(2, 2, 1)
  38. }).to(0.3, {
  39. scale: v3(1, 1, 1)
  40. }).to(time, {
  41. position: posEnd
  42. }, {
  43. easing: "cubicOut"
  44. }).call(() => {
  45. count++;
  46. coinNode.destroy();
  47. if (count >= num) {
  48. this.closeLayer();
  49. }
  50. }).start();
  51. }
  52. audioManager.instance.playEffect(constants.audioNames.gold);
  53. }
  54. }