FlyCoin.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { cocosz } from "./CocosZ";
  2. import Constant from "./Constant";
  3. /**
  4. * 收取金币效果
  5. */
  6. export default class FlyCoin {
  7. /**
  8. * 金币效果展示方法
  9. * @param pos 坐标
  10. * @param group 分组
  11. * @param callFun 所有金币飞到后回调
  12. */
  13. public static Show(iconName: string, pos: cc.Vec3, group?: string, callFun?: Function) {
  14. let pre = cocosz.resMgr.getRes("FlyIcon", cc.Prefab);
  15. if (pre) {
  16. let finshCount: number = 0;
  17. for (let i = 0; i < 15; i++) {
  18. const node: cc.Node = cc.instantiate(pre);
  19. if (node) {
  20. node.children.forEach(c => {
  21. if (c.name == iconName) {
  22. c.opacity = 255;
  23. } else {
  24. c.opacity = 0;
  25. }
  26. })
  27. if (group) node.group = group;
  28. node.position = cc.v3(cc.winSize.width * 0.5, cc.winSize.height * 0.5);
  29. node.scale = 2;
  30. cc.director.getScene().addChild(node, 10000);
  31. const t: cc.Tween = cc.tween(node);
  32. t.by(0.2, { x: Math.random() * 400 - 200, y: Math.random() * 400 - 200 });
  33. t.delay(Math.random() * 0.5 + 0.2);
  34. t.parallel(cc.tween().to(0.3, { position: pos }), cc.tween().to(0.3, { scale: 0 }));
  35. t.call(() => {
  36. finshCount++;
  37. if (finshCount == 14) {
  38. callFun && callFun();
  39. }
  40. cocosz.audioMgr.playEffect(iconName);
  41. node.destroy();
  42. });
  43. t.start();
  44. }
  45. }
  46. } else {
  47. cc.log("Prefab FlyCoin is not found!");
  48. }
  49. };
  50. }