目录— 个文件
⌕
目录加载中…
…/module/ani/AutoShake.ts
1import { _decorator, Component, Vec3, tween, Tween, Node } from 'cc';
2
3const { ccclass, property } = _decorator;
4
5/**
6 * 挂在节点上,让节点做轻微左右「摇头」的旋转抖动(适合礼包/按钮高亮)
7 * - 位置不变,只绕 Z 轴轻微旋转
8 * - 可配置每次抖动的间隔时间(秒)
9 * - 可配置抖动角度
10 */
11@ccclass('AutoShake')
12export class AutoShake extends Component {
13
14 @property({
15 tooltip: '两次抖动之间的间隔时间(秒,抖动完成后再等待)',
16 })
17 public interval: number = 1.2;
18
19 @property({
20 tooltip: '左右摆动的最大角度(度),例如 5 表示 ±5°',
21 min: 0.0,
22 })
23 public amplitude: number = 5;
24
25 @property({
26 tooltip: '单次完整抖动(右→左→回中)的总时间(秒)',
27 })
28 public shakeDuration: number = 0.3;
29
30 private _originalPos: Vec3 = new Vec3();
31 private _originalRotationZ: number = 0;
32 private _shakeTween: Tween<Node> | null = null;
33
34 onEnable() {
35 if (this.node) {
36 this.node.getPosition(this._originalPos);
37 // 记录初始角度
38 this._originalRotationZ = this.node.eulerAngles.z;
39 }
40 this.StartShakeTween();
41 }
42
43 onDisable() {
44 // 还原位置,避免节点永久偏移
45 if (this.node) {
46 this.node.setPosition(this._originalPos);
47 // 还原旋转角度
48 const euler = this.node.eulerAngles;
49 this.node.setRotationFromEuler(euler.x, euler.y, this._originalRotationZ);
50 }
51 this._shakeTween?.stop();
52 this._shakeTween = null;
53 }
54
55 /**
56 * 使用 tween 持续抖动
57 */
58 private StartShakeTween() {
59 if (!this.node || this.interval <= 0) {
60 return;
61 }
62
63 if (this._shakeTween) {
64 this._shakeTween.stop();
65 this._shakeTween = null;
66 }
67
68 const node = this.node;
69 const originPos = this._originalPos.clone();
70 const originRotZ = this._originalRotationZ;
71 const total = Math.max(this.shakeDuration, 0.05);
72 const half = total / 2;
73 const gap = Math.max(this.interval, 0.01);
74
75 // 左右轻微旋转抖动:回到原角度 -> 向右旋转 -> 向左旋转 -> 回到原角度 -> 等待 gap 秒
76 this._shakeTween = tween(node)
77 .repeatForever(
78 tween(node)
79 // 回到原始位置与角度,防止误差累积
80 .call(() => {
81 node.setPosition(originPos);
82 const euler = node.eulerAngles;
83 node.setRotationFromEuler(euler.x, euler.y, originRotZ);
84 })
85 // 向右旋转(正角度)
86 .to(half, { eulerAngles: new Vec3(0, 0, originRotZ + this.amplitude) })
87 // 向左旋转(负角度)
88 .to(half, { eulerAngles: new Vec3(0, 0, originRotZ - this.amplitude) })
89 // 回到中间角度
90 .to(half, { eulerAngles: new Vec3(0, 0, originRotZ) })
91 // 间隔一段时间再下一次抖动
92 .delay(gap)
93 );
94
95 this._shakeTween.start();
96 }
97}
98
讲解99 行
旋转摇摆人工通读
AutoShake.ts
每次启用采样节点位置与 Z 轴角度,随后构建重复的右摆、左摆、回中动画。实例保存 tween 和还原值,禁用会恢复位置与角度;三段各用标称时长的一半,因此一次摆动实际是一点五倍配置时长,间隔非正则完全不启动。
对比 onEnable 的采样、StartShakeTween 的三段时长与 onDisable 的还原。
特殊处理与易错点
- 易错点shakeDuration 被平分后使用三次,实际总时长不是配置标称值。
- 特殊处理动画目标欧拉角固定 X/Y 为零;禁用只恢复 Z,X/Y 取禁用时当前值。
11注释挂在节点上,让节点做轻微左右「摇头」的旋转抖动(适合礼包/按钮高亮) - 位置不变,只绕 Z 轴轻微旋转 - 可配置每次抖动的间隔时间(秒) - 可配置抖动角度
12注释AutoShake:挂在节点上,让节点做轻微左右「摇头」的旋转抖动(适合礼包/按钮高亮) - 位置不变,只绕 Z
17属性interval(number):该模块的运行态字段。
23属性amplitude(number):该模块的运行态字段。
28属性shake Duration(number):该模块的运行态字段。
30属性original Pos(Vec3):该模块的运行态字段。
31属性启用时采样的 Z 轴角度,各段旋转围绕该值展开。
32属性shake Tween(Tween<Node> | null):该模块的运行态字段。
34方法记录当前位置与原始旋转角后创建周期摇摆动画。
38注释记录初始角度
43方法恢复原始位置和旋转角并停止保存的摇摆动画。
45注释还原位置,避免节点永久偏移
48注释还原旋转角度
58方法间隔为正时重建右摆左摆回中并等待的循环动画。
76注释左右轻微旋转抖动:回到原角度 -> 向右旋转 -> 向左旋转 -> 回到原角度 -> 等待 gap 秒
80注释回到原始位置与角度,防止误差累积
86注释向右旋转(正角度)
88注释向左旋转(负角度)
90注释回到中间角度
92注释间隔一段时间再下一次抖动