| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- import { _decorator, Component, Node, Animation} from 'cc';
- const { ccclass, property } = _decorator;
- @ccclass('Tree')
- export class Tree extends Component {
- public animationComp: Animation | null = null;
- start() {
- // 获取当前节点上的 Animation 组件
- this.animationComp = this.node.getComponent(Animation);
- if (!this.animationComp) {
- console.error("当前节点没有 Animation 组件!");
- return;
- }
- else
- {
- console.log("Animation Loaded");
- }
- }
- update(deltaTime: number) {
-
- }
- public shakeAni()
- {
- // 示例:根据动画名称获取剪辑并播放
- const targetClipName = "Shake"; // 替换为你的动画剪辑名称
- const targetClip = this.getClipByName(targetClipName);
-
- if (targetClip) {
- console.log(`Found anim clip:${targetClipName},start play`);
- this.animationComp.play(targetClipName); // 直接用名称播放,或用剪辑实例播放
- // 也可以通过剪辑实例获取更多信息,例如时长
- console.log(`Duration:${targetClip.duration} second`);
- } else {
- console.error(`Not found ${targetClipName} ani clip!`);
- }
- }
- /**
- * 根据动画剪辑名称,从当前节点的 Animation 组件中查找剪辑
- * @param clipName 动画剪辑的名称(区分大小写)
- * @returns 找到的 AnimationClip 实例,或 null
- */
- private getClipByName(clipName: string): AnimationClip | null {
- if (!this.animationComp) return null;
- // 遍历 Animation 组件的 clips 数组
- for (const clip of this.animationComp.clips) {
- if (clip.name === clipName) {
- return clip;
- }
- }
- return null;
- }
- }
|