import { _decorator, Component, Node, SkeletalAnimation, AnimationClip, CCBoolean } from 'cc'; const { ccclass, property } = _decorator; @ccclass('RoleController') export class RoleController extends Component { @property(SkeletalAnimation) skeletalAnimation: SkeletalAnimation = null; @property(AnimationClip) idleClip: AnimationClip = null; @property(AnimationClip) moveClip: AnimationClip = null; @property(AnimationClip) attackClip: AnimationClip = null; @property(AnimationClip) deathClip: AnimationClip = null; @property({type: CCBoolean}) islog: boolean = false; private currentAnimIndex: number = 0; private animationNames: string[] = []; private animationClips: AnimationClip[] = []; start() { // 将外部传入的动画剪辑添加到骨骼动画组件 if (this.skeletalAnimation) { // 添加待机动画 if (this.idleClip) { this.skeletalAnimation.addClip(this.idleClip,"idle"); this.animationClips.push(this.idleClip); this.animationNames.push("idle"); } // 添加移动动画 if (this.moveClip) { this.skeletalAnimation.addClip(this.moveClip, "move"); this.animationClips.push(this.moveClip); this.animationNames.push("move"); } // 添加攻击动画 if (this.attackClip) { this.skeletalAnimation.addClip(this.attackClip, "attack"); this.animationClips.push(this.attackClip); this.animationNames.push("attack"); } // 添加死亡动画 if (this.deathClip) { this.skeletalAnimation.addClip(this.deathClip, "death"); this.animationClips.push(this.deathClip); this.animationNames.push("death"); } if (this.animationClips.length > 0) { // 播放第一个动画 this.skeletalAnimation.play(this.animationNames[0]); this.log(`当前动画: ${this.animationNames[0]}`); } } } /** * 播放指定动作的动画 * @param action 动作名称: "idle" | "move" | "attack" | "death" * @param loop 是否循环播放 */ public PlayAnimation(action: string, loop: boolean = true): void { if (!this.skeletalAnimation) { console.warn('SkeletalAnimation 组件未设置'); return; } // 检查动画是否存在 if (!this.animationNames.includes(action)) { console.warn(`动画 "${action}" 不存在,可用动画: ${this.animationNames.join(', ')}`); return; } // 获取动画状态并设置循环 const animState = this.skeletalAnimation.getState(action); if (animState) { animState.wrapMode = loop ? AnimationClip.WrapMode.Loop : AnimationClip.WrapMode.Normal; } // 播放动画 this.skeletalAnimation.play(action); this.log(`播放动画: ${action}, 循环: ${loop}`); } update(deltaTime: number) { } private log(...args: any[]): void { if (this.islog){ console.log('[RoleController]', ...args); } } }