ShowRedPrizeAnim.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { _decorator, Component, Node, Animation, EventHandler, Button, Prefab, instantiate, Vec3 } from 'cc';
  2. import { ResourceLoader } from '../core/resourceManager/ResourceLoader';
  3. import { Sound } from '../core/sound/Sound';
  4. import { Firework } from './Firework';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('ShowRedPrizeAnim')
  7. export class ShowRedPrizeAnim extends Component {
  8. @property({ tooltip: "音效组件", type: Sound }) private sound: Sound;
  9. @property({ tooltip: "背景", type: Button }) private bg: Button;
  10. @property({ tooltip: "烟花节点", type: Node }) private fireworkNode: Node;
  11. start() {
  12. }
  13. public animEndBack() {
  14. this.sound.play(1);
  15. this.bg.interactable = true;
  16. this.effect();
  17. }
  18. //烟花特效
  19. private async effect() {
  20. let r = 180;
  21. let rRandom = 180;
  22. let positionArray = [];
  23. for (let i = 1; i < 15; i++) {
  24. let rotation = 90 * Math.random();
  25. let y = Math.sin(rotation * ((2 * Math.PI) / 360)) * r;
  26. let x = Math.cos(rotation * ((2 * Math.PI) / 360)) * r;
  27. positionArray.push({ x: x + (Math.random() * rRandom), y: y + (Math.random() * rRandom) });
  28. positionArray.push({ x: -x - (Math.random() * rRandom), y: y + (Math.random() * rRandom) });
  29. positionArray.push({ x: x + (Math.random() * rRandom), y: -y - (Math.random() * rRandom) });
  30. positionArray.push({ x: -x - (Math.random() * rRandom), y: -y - (Math.random() * rRandom) });
  31. }
  32. let load = this.getComponent(ResourceLoader);
  33. for (let i = 0; i < positionArray.length; i++) {
  34. let prefab = await load.load<Prefab>("prefabs/ui/fireworkEffect", Prefab);
  35. let firework = instantiate(prefab);
  36. firework.setPosition(new Vec3(0, 0, 0));
  37. firework.parent = this.fireworkNode;
  38. firework.getComponent(Firework).playEffect(positionArray[i]);
  39. }
  40. }
  41. }