Firework.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { _decorator, Component, Node, Vec3, EventHandler, Animation, SpriteFrame, Sprite } from 'cc';
  2. import { ResourceLoader } from '../core/resourceManager/ResourceLoader';
  3. import { UITween } from '../core/ui/tween/UITween';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('Firework')
  6. export class Firework extends Component {
  7. @property({ tooltip: "缓动动画", type: UITween }) private uiTween: UITween;
  8. // @property({ tooltip: "飘落动画启动回调", type: EventHandler }) private animStartBack: EventHandler[] = [];
  9. // @property({ tooltip: "飘落动画结束回调", type: EventHandler }) private animEndBack: EventHandler[] = [];
  10. start() {
  11. }
  12. public async playEffect(position: { x: number, y: number }) {
  13. let imgType = Math.ceil(Math.random() * 7);
  14. let img = await this.node.getComponent(ResourceLoader).load<SpriteFrame>("image/firework/" + imgType + "/spriteFrame", SpriteFrame);
  15. this.node.getComponent(Sprite).spriteFrame = img;
  16. let tempRotation = 360;
  17. this.uiTween.targetData[0].propsData.position = new Vec3(position.x, position.y);
  18. this.uiTween.targetData[0].duration = 0.3;
  19. this.uiTween.targetData[1].propsData.position = new Vec3(position.x, position.y - (180 + Math.random() * 100));
  20. this.uiTween.targetData[1].propsData.eulerAngles =
  21. new Vec3((Math.random() - Math.random()) * tempRotation, (Math.random() - Math.random()) * tempRotation, (Math.random() - Math.random()) * tempRotation);
  22. this.uiTween.targetData[1].duration = Math.random() * 0.5 + 0.5;
  23. this.scheduleOnce(() => {
  24. this.uiTween.play();
  25. })
  26. }
  27. public playAnim() {
  28. this.node.getComponent(Animation).play();
  29. }
  30. public endDestory() {
  31. this.node.destroy();
  32. }
  33. }