在开发游戏的时候,经常要对人物的动作进行控制,这时如果使用常规的方法很容易出现重复播放某一个动画,导致人物动作卡住的情况;
这时就要用状态来控制;
在class内加入相应的状态方法:
//在class内加入
_state: AIState = AIState._00_等待;
get state() {
return this._state;
}
set state(value: AIState) {
this._state = value;
switch (this._state) {
case AIState._00_等待:
cc.find('RootNode', this.node).getComponent(cc.Animation).play('等待');
break;
case AIState._01_移动:
cc.find('RootNode', this.node).getComponent(cc.Animation).play('跑中');
break;
case AIState._02_攻击:
cc.find('RootNode', this.node).getComponent(cc.Animation).play('打');
break;
case AIState._03_死亡:
let anim = cc.find('RootNode', this.node).getComponent(cc.Animation).play('躺');
anim.repeatCount = 1;//死亡动画只播放1次
}
}
在class外添加对应状态:
export enum AIState {
_00_等待,
_01_移动,
_02_攻击,
_03_死亡,
_04_准备移动,
_05_结束移动,
}
使用时只要赋值一下,就会进入对应方法,执行对应动作,防止重复某一动作:
this.state == AIState._03_死亡;//切换到死亡动画
if (this.state != AIState._02_攻击) {//判断一下当前是否正在播放攻击动画
this.state == AIState._02_攻击;//不是就可以切换到攻击动画
}
本文介绍了在游戏开发中如何通过状态机避免人物动作卡住的问题。通过定义AIState枚举并结合getter和setter方法,实现不同状态(等待、移动、攻击、死亡)下的人物动画切换。例如,当状态切换到死亡时,死亡动画只播放一次。这种方法确保了动作流畅且不会重复播放。
2012

被折叠的 条评论
为什么被折叠?



