找物源码导读Cocos Creator 3.8 · assets/scripts
0%
已读 0 / 424
目录— 个文件
目录加载中…
…/module/ani/AutoBouns.ts
1// import { _decorator, Component, Vec3, tween, Tween, Node } from 'cc';
2
3// const { ccclass, property } = _decorator;
4
5// /**
6// * 挂在节点上,让节点做上下的缩放效果(适合礼包/按钮高亮)
7// * - 位置不变,只缩放 Y 轴
8// * - 可配置每次抖动的间隔时间(秒)
9// * - 可配置抖动角度
10// */
11// @ccclass('AutoBouns')
12// export class AutoBouns 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 bounsDuration: 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 行
停用草稿人工通读

AutoBouns.ts

文件全文都是行注释,保留了旧版节点摇摆动画的草稿,当前不会导入 Cocos、注册组件或执行生命周期。草稿字段写的是 bounsDuration,内部却引用 shakeDuration;不能按文件名误认成正在运行的上下缩放组件。

先确认整份文件的行注释,再对比草稿时长字段与 tween 旋转属性。

特殊处理与易错点

  • 特殊处理没有任何可执行导出,当前不会生成 AutoBouns 组件。
  • 易错点注释草稿实际旋转 Z 轴,并且引用未声明的 shakeDuration。
这个文件没有可解析的注释,直接读代码即可。