SkillButtonAnimator.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. /**
  11. * 播放缩小并(可选)移动到指定目标位置的动画。
  12. * @param duration 动画时长(秒)
  13. * @param targetPos 目标位置(节点本地坐标系),不传则保持原位置
  14. * @param onComplete 完成回调
  15. */
  16. public playShrink(duration: number = 0.3, targetPos?: Vec3, onComplete?: () => void) {
  17. // 若目标位置存在,则先 tween 到该位置;否则只缩放
  18. const props: any = { scale: new Vec3(0, 0, 0) };
  19. if (targetPos) {
  20. props.position = targetPos;
  21. }
  22. tween(this.node)
  23. .to(duration, props, { easing: 'quadIn' })
  24. .call(() => {
  25. this.node.active = false; // 动画结束后隐藏节点
  26. if (onComplete) {
  27. onComplete();
  28. }
  29. })
  30. .start();
  31. }
  32. }
  33. export default SkillButtonAnimator;