SkillButtonAnimator.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { _decorator, Component, Node, Vec3, tween } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. /**
  4. * SkillButtonAnimator
  5. * 负责技能按钮的缩放 / 移动动画。
  6. * 提供 playShrink 接口供外部调用。
  7. */
  8. @ccclass('SkillButtonAnimator')
  9. export class SkillButtonAnimator extends Component {
  10. private _origScale: Vec3 = new Vec3();
  11. private _origPos: Vec3 = new Vec3();
  12. onLoad () {
  13. // 记录初始位置、缩放,用于恢复
  14. this._origScale.set(this.node.scale);
  15. this._origPos.set(this.node.position);
  16. }
  17. /**
  18. * 播放缩小并(可选)移动到指定目标位置的动画。
  19. * @param duration 动画时长(秒)
  20. * @param targetPos 目标位置(节点本地坐标系),不传则保持原位置
  21. * @param onComplete 完成回调
  22. */
  23. public playShrink(duration: number = 0.3, targetPos?: Vec3, onComplete?: () => void) {
  24. // 若目标位置存在,则先 tween 到该位置;否则只缩放
  25. const props: any = { scale: new Vec3(0, 0, 0) };
  26. if (targetPos) {
  27. props.position = targetPos;
  28. }
  29. tween(this.node)
  30. .to(duration, props, { easing: 'quadIn' })
  31. .call(() => {
  32. // 动画结束后隐藏节点,但允许后续 resetState 恢复
  33. this.node.active = false;
  34. if (onComplete) onComplete();
  35. })
  36. .start();
  37. this.node.setPosition(this._origPos);
  38. }
  39. public resetState() {
  40. // Reset scale, position, opacity, etc. as needed
  41. this.node.setScale(Vec3.ONE);
  42. this.node.active = true;
  43. // this.node.setPosition(this._initPos);
  44. // this.node.opacity = 255;
  45. }
  46. }
  47. export default SkillButtonAnimator;