骨骼动画播放问题

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

View File

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

View File

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

View File

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

View File

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

View File

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