骨骼动画播放问题

This commit is contained in:
xukuan 2025-03-01 16:55:28 +08:00
parent dcfaf26c72
commit 97d5bf0588
6 changed files with 43 additions and 19 deletions

View File

@ -1,6 +1,15 @@
import { _decorator, Component, Node, SkeletalAnimation, Input, EventTouch, input, Label } from 'cc'; import { _decorator, Component, Node, SkeletalAnimation, Input, EventTouch, input, Label } from 'cc';
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
/** 玩家状态 */
enum EPlayerState {
IDLE,
WALK,
RUN,
DIE,
VICTORY
}
@ccclass('WoodenManPlayer') @ccclass('WoodenManPlayer')
export class WoodenManPlayer extends Component { export class WoodenManPlayer extends Component {
@ -15,6 +24,7 @@ export class WoodenManPlayer extends Component {
private readonly WALK_SPEED = 3; // 走路速度 private readonly WALK_SPEED = 3; // 走路速度
private readonly RUN_SPEED = 6; // 跑步速度 private readonly RUN_SPEED = 6; // 跑步速度
private isStoppingMove: boolean = false; // 添加在类的开头 private isStoppingMove: boolean = false; // 添加在类的开头
private _playerState: EPlayerState = EPlayerState.IDLE;
@property(Node) @property(Node)
@ -29,13 +39,12 @@ export class WoodenManPlayer extends Component {
this.id = id; this.id = id;
this.isLocalPlayer = isLocal; this.isLocalPlayer = isLocal;
this.animator = this.getComponentInChildren(SkeletalAnimation); this.animator = this.getComponentInChildren(SkeletalAnimation);
if (!this.animator) { if (!this.animator) {
console.error(`Player ${id} 缺少 SkeletalAnimation 组件`); console.error(`Player ${id} 缺少 SkeletalAnimation 组件`);
} else { } else {
const idleState = this.animator.getState('player_idle'); const idleState = this.animator.getState('player_idle1');
if (idleState) { if (idleState && this._playerState === EPlayerState.IDLE) {
this.animator.crossFade('player_idle', 0.1); this.animator.crossFade('player_idle1', 0.1);
} }
} }
} }
@ -57,7 +66,8 @@ export class WoodenManPlayer extends Component {
if (!alive) { if (!alive) {
// 死亡时停止所有动画和移动 // 死亡时停止所有动画和移动
this.isMoving = false; this.isMoving = false;
if (this.animator) { this._playerState = EPlayerState.DIE;
if (this._getAnimator()) {
this.animator.stop(); // 停止当前动画 this.animator.stop(); // 停止当前动画
} }
} }
@ -72,10 +82,11 @@ export class WoodenManPlayer extends Component {
if (this.hasPlayedDieAnimation) return; if (this.hasPlayedDieAnimation) return;
// 先停止当前动画 // 先停止当前动画
if (this.animator) { if (this._getAnimator()) {
this.animator.stop(); this.animator.stop();
const state = this.animator.getState('player_die'); const state = this.animator.getState('player_die');
if (state) { if (state) {
state.wrapMode = 1; // 1 表示只播放一次
this.animator.crossFade('player_die', 0.5); this.animator.crossFade('player_die', 0.5);
if (callback) { if (callback) {
state.once(SkeletalAnimation.EventType.FINISHED, callback); state.once(SkeletalAnimation.EventType.FINISHED, callback);
@ -95,10 +106,10 @@ export class WoodenManPlayer extends Component {
// 播放行走动画 // 播放行走动画
playWalkAnimation() { playWalkAnimation() {
if (this.animator) { if (this._getAnimator()) {
// 使用 play 而不是 crossFade并设置 wrapMode 为循环
const state = this.animator.getState('player_walk'); const state = this.animator.getState('player_walk');
if (state) { if (state) {
this._playerState = EPlayerState.WALK;
state.wrapMode = 2; // 2 表示循环模式 state.wrapMode = 2; // 2 表示循环模式
this.animator.play('player_walk'); this.animator.play('player_walk');
} }
@ -107,10 +118,10 @@ export class WoodenManPlayer extends Component {
// 播放跑步动画 // 播放跑步动画
playRunAnimation() { playRunAnimation() {
if (this.animator) { if (this._getAnimator()) {
// 使用 play 而不是 crossFade并设置 wrapMode 为循环
const state = this.animator.getState('player_run'); const state = this.animator.getState('player_run');
if (state) { if (state) {
this._playerState = EPlayerState.RUN;
state.wrapMode = 2; // 2 表示循环模式 state.wrapMode = 2; // 2 表示循环模式
this.animator.play('player_run'); this.animator.play('player_run');
} }
@ -124,8 +135,9 @@ export class WoodenManPlayer extends Component {
this.isStoppingMove = true; // 设置标志 this.isStoppingMove = true; // 设置标志
this.isMoving = false; this.isMoving = false;
this.moveTimer = 0; this.moveTimer = 0;
if (this.animator) { this._playerState = EPlayerState.IDLE;
this.animator.crossFade('player_idle', 0.3); if (this._getAnimator()) {
this.animator.crossFade('player_idle1', 0.3);
} }
// 同步按钮状态 // 同步按钮状态
if (this.isMovingByButton && this.buttonLabel) { if (this.isMovingByButton && this.buttonLabel) {
@ -155,13 +167,24 @@ export class WoodenManPlayer extends Component {
} }
} }
private _getAnimator() {
if (!this.animator) {
this.animator = this.getComponentInChildren(SkeletalAnimation);
if (!this.animator) {
console.error(`Player ${this.id} 缺少 SkeletalAnimation 组件`);
}
}
return this.animator;
}
public startMoving() { public startMoving() {
if (!this.isAlive() || this.isMoving) return; if (!this.isAlive() || this.isMoving) return;
this.isMoving = true; this.isMoving = true;
this.moveTimer = 0; // 重置计时器 this.moveTimer = 0; // 重置计时器
if (this.animator) { if (this._getAnimator()) {
this.playWalkAnimation(); this.playWalkAnimation();
} }
} }
@ -171,7 +194,8 @@ export class WoodenManPlayer extends Component {
this.moveTimer += dt; this.moveTimer += dt;
// 根据移动时间切换动画 // 根据移动时间切换动画
if (this.moveTimer >= 1 && this.animator) { if (this.moveTimer >= 1 && this._getAnimator() && this._playerState !== EPlayerState.RUN) {
this._playerState = EPlayerState.RUN;
this.animator.getState('player_run').wrapMode = 2; this.animator.getState('player_run').wrapMode = 2;
this.animator.crossFade('player_run', 0.3); this.animator.crossFade('player_run', 0.3);
} }

View File

@ -564,7 +564,7 @@
"propertyPath": [ "propertyPath": [
"playOnLoad" "playOnLoad"
], ],
"value": false "value": true
}, },
{ {
"__type__": "CCPropertyOverrideInfo", "__type__": "CCPropertyOverrideInfo",

View File

@ -464,7 +464,7 @@
"propertyPath": [ "propertyPath": [
"playOnLoad" "playOnLoad"
], ],
"value": false "value": true
}, },
{ {
"__type__": "cc.TargetInfo", "__type__": "cc.TargetInfo",

View File

@ -464,7 +464,7 @@
"propertyPath": [ "propertyPath": [
"playOnLoad" "playOnLoad"
], ],
"value": false "value": true
}, },
{ {
"__type__": "cc.TargetInfo", "__type__": "cc.TargetInfo",

View File

@ -464,7 +464,7 @@
"propertyPath": [ "propertyPath": [
"playOnLoad" "playOnLoad"
], ],
"value": false "value": true
}, },
{ {
"__type__": "cc.TargetInfo", "__type__": "cc.TargetInfo",

View File

@ -464,7 +464,7 @@
"propertyPath": [ "propertyPath": [
"playOnLoad" "playOnLoad"
], ],
"value": false "value": true
}, },
{ {
"__type__": "cc.TargetInfo", "__type__": "cc.TargetInfo",