refactor(map): 优化英雄切换动画及状态管理
- 将 HInfoComp 组件改为继承自 CCComp 并注册为 ECS 组件 - 新增动画锁定标志 isMoving 防止快速点击引起动画冲突 - 添加 moveTimeoutId 用于管理动画队列异步操作,避免重叠 - 优化英雄切换的移动动画,缩短动画时长为0.2秒 - moveHeroesLeft 与 moveHeroesRight 方法增加动画锁定与异步取消逻辑 - 在切换英雄时调用 smc.updateFihgtHero 以更新当前战斗英雄状态 - 清理和销毁动画节点时更严格以避免残留和内存泄漏 - MissionHomeComp 中 mission_end 方法增加日志输出 - MissionHeroComp 去除了冗余空行,优化代码结构
This commit is contained in:
@@ -2,10 +2,14 @@ import { _decorator, Animation, AnimationClip, Component, instantiate, Label, No
|
||||
import { oops } from 'db://oops-framework/core/Oops';
|
||||
import { getHeroList, HeroInfo, HType, HTypeName } from '../common/config/heroSet';
|
||||
import { smc } from '../common/SingletonModuleComp';
|
||||
import { GameEvent } from '../common/config/GameEvent';
|
||||
import { CCComp } from 'db://oops-framework/module/common/CCComp';
|
||||
import { ecs } from 'db://oops-framework/libs/ecs/ECS';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('HInfoComp')
|
||||
export class HInfoComp extends Component {
|
||||
@ecs.register('HInfoComp', false)
|
||||
export class HInfoComp extends CCComp {
|
||||
h_uuid:number=0
|
||||
name_node:any=null
|
||||
type_node:any=null
|
||||
@@ -27,6 +31,13 @@ export class HInfoComp extends Component {
|
||||
6:v3(-420,-109,0), // 不在屏幕内
|
||||
}
|
||||
|
||||
// 动画锁定标志:防止快速点击导致的动画冲突
|
||||
private isMoving: boolean = false;
|
||||
// 保存待处理的setTimeout句柄,用于取消前一个异步操作
|
||||
private moveTimeoutId: any = null;
|
||||
protected onLoad(): void {
|
||||
// this.on(GameEvent.MissionStart,this.mission_start,this)
|
||||
}
|
||||
// 位置索引常量
|
||||
private static center_pos = 3;
|
||||
|
||||
@@ -105,7 +116,9 @@ export class HInfoComp extends Component {
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
mission_start(){
|
||||
this.claear_hero()
|
||||
}
|
||||
claear_hero(){
|
||||
for (let i = 0; i < this.heroNodes.length; i++) {
|
||||
if (this.heroNodes[i]) {
|
||||
@@ -116,6 +129,9 @@ export class HInfoComp extends Component {
|
||||
}
|
||||
|
||||
next_hero(){
|
||||
// 检查是否正在动画中,防止快速点击导致的冲突
|
||||
if (this.isMoving) return;
|
||||
|
||||
// 获取英雄列表
|
||||
let heros = getHeroList();
|
||||
let index = heros.indexOf(this.h_uuid);
|
||||
@@ -125,6 +141,7 @@ export class HInfoComp extends Component {
|
||||
|
||||
// 更新数据
|
||||
this.h_uuid = nextHero;
|
||||
smc.updateFihgtHero(nextHero)
|
||||
this.update_data(nextHero);
|
||||
|
||||
// 执行平滑移动动画
|
||||
@@ -132,6 +149,9 @@ export class HInfoComp extends Component {
|
||||
}
|
||||
|
||||
prev_hero(){
|
||||
// 检查是否正在动画中,防止快速点击导致的冲突
|
||||
if (this.isMoving) return;
|
||||
|
||||
// 获取英雄列表
|
||||
let heros = getHeroList();
|
||||
let index = heros.indexOf(this.h_uuid);
|
||||
@@ -141,6 +161,7 @@ export class HInfoComp extends Component {
|
||||
|
||||
// 更新数据
|
||||
this.h_uuid = prevHero;
|
||||
smc.updateFihgtHero(prevHero)
|
||||
this.update_data(prevHero);
|
||||
|
||||
// 执行平滑移动动画
|
||||
@@ -148,6 +169,14 @@ export class HInfoComp extends Component {
|
||||
}
|
||||
|
||||
moveHeroesLeft() {
|
||||
// 取消前一个待处理的异步操作
|
||||
if (this.moveTimeoutId !== null) {
|
||||
clearTimeout(this.moveTimeoutId);
|
||||
}
|
||||
|
||||
// 设置动画锁定
|
||||
this.isMoving = true;
|
||||
|
||||
// 在动画开始前,直接销毁hero_pos[6]上的节点
|
||||
if (this.heroNodes[6]) {
|
||||
this.heroNodes[6].destroy();
|
||||
@@ -162,13 +191,13 @@ export class HInfoComp extends Component {
|
||||
|
||||
// 使用Tween执行平滑移动
|
||||
tween(this.heroNodes[i])
|
||||
.to(0.3, { position: targetPos })
|
||||
.to(0.2, { position: targetPos })
|
||||
.start();
|
||||
}
|
||||
}
|
||||
|
||||
// 延迟重排数组和创建新节点,等待动画完成
|
||||
setTimeout(() => {
|
||||
this.moveTimeoutId = setTimeout(() => {
|
||||
// 移动数组元素(向后平移)
|
||||
for (let i = 6; i > 0; i--) {
|
||||
this.heroNodes[i] = this.heroNodes[i - 1];
|
||||
@@ -185,10 +214,22 @@ export class HInfoComp extends Component {
|
||||
}
|
||||
|
||||
this.heroNodes[0] = this.load_hui(heros[newIndex], 0);
|
||||
}, 300); // 与动画时间保持一致
|
||||
|
||||
// 动画完成,解除锁定
|
||||
this.isMoving = false;
|
||||
this.moveTimeoutId = null;
|
||||
}, 200); // 与动画时间保持一致
|
||||
}
|
||||
|
||||
moveHeroesRight() {
|
||||
// 取消前一个待处理的异步操作
|
||||
if (this.moveTimeoutId !== null) {
|
||||
clearTimeout(this.moveTimeoutId);
|
||||
}
|
||||
|
||||
// 设置动画锁定
|
||||
this.isMoving = true;
|
||||
|
||||
// 在动画开始前,直接销毁hero_pos[0]上的节点
|
||||
if (this.heroNodes[0]) {
|
||||
this.heroNodes[0].destroy();
|
||||
@@ -203,13 +244,13 @@ export class HInfoComp extends Component {
|
||||
|
||||
// 使用Tween执行平滑移动
|
||||
tween(this.heroNodes[i])
|
||||
.to(0.3, { position: targetPos })
|
||||
.to(0.2, { position: targetPos })
|
||||
.start();
|
||||
}
|
||||
}
|
||||
|
||||
// 延迟重排数组和创建新节点,等待动画完成
|
||||
setTimeout(() => {
|
||||
this.moveTimeoutId = setTimeout(() => {
|
||||
// 移动数组元素(向前平移)
|
||||
for (let i = 0; i < 6; i++) {
|
||||
this.heroNodes[i] = this.heroNodes[i + 1];
|
||||
@@ -226,7 +267,11 @@ export class HInfoComp extends Component {
|
||||
}
|
||||
|
||||
this.heroNodes[6] = this.load_hui(heros[newIndex], 6);
|
||||
}, 300); // 与动画时间保持一致
|
||||
|
||||
// 动画完成,解除锁定
|
||||
this.isMoving = false;
|
||||
this.moveTimeoutId = null;
|
||||
}, 200); // 与动画时间保持一致
|
||||
}
|
||||
|
||||
reset() {
|
||||
|
||||
@@ -56,15 +56,11 @@ export class MissionHeroCompComp extends CCComp {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private zhao_huan(event: string, args: any){
|
||||
// console.log("[MissionHeroComp]:zhaohuan",args)
|
||||
this.addHero(args.uuid,false)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** 添加英雄 */
|
||||
private addHero(uuid:number=1001,is_zhaohuan:boolean=false) {
|
||||
// console.log("[MissionHeroComp]:addHero",uuid,is_zhaohuan)
|
||||
|
||||
@@ -27,11 +27,11 @@ export class MissionHomeComp extends CCComp {
|
||||
|
||||
|
||||
start_mission() {
|
||||
|
||||
oops.message.dispatchEvent(GameEvent.MissionStart, {})
|
||||
this.node.active=false;
|
||||
}
|
||||
mission_end(){
|
||||
console.log("[MissionHomeComp]=>mission_end")
|
||||
this.home_active()
|
||||
}
|
||||
home_active(){
|
||||
|
||||
Reference in New Issue
Block a user