refactor(map): 优化技能盒与装备盒的长按交互逻辑

将原有的单点触摸触发改为长按触发,统一使用触摸开始/结束/取消事件实现长按检测,添加长按计时和状态标记,修复短按误触发的问题,同时在组件销毁时清理计时器防止内存泄漏。
This commit is contained in:
panFD
2026-07-26 20:13:41 +08:00
parent c7afe5d73f
commit cb2bc37a68
2 changed files with 70 additions and 8 deletions

View File

@@ -122,7 +122,10 @@ export class SkillBoxComp extends CCComp {
oops.message.on(GameEvent.MissionEnd, this.onMissionEnd, this);
this.node.on(GameEvent.NewWave, this.onNewWave, this);
oops.message.on(GameEvent.NewWave, this.onNewWaveGlobal, this);
this.node.on(NodeEventType.TOUCH_END, this.onNodeClicked, this);
// 长按弹出信息框:按下启动计时,放开/取消时关闭
this.node.on(NodeEventType.TOUCH_START, this.onTouchStart, this);
this.node.on(NodeEventType.TOUCH_END, this.onTouchEnd, this);
this.node.on(NodeEventType.TOUCH_CANCEL, this.onTouchEnd, this);
}
/** 销毁时移除所有事件监听并通知槽位管理器回收 */
@@ -139,17 +142,45 @@ export class SkillBoxComp extends CCComp {
oops.message.off(GameEvent.ReviveSuccess, this.onEventTrigger, this);
if (this.node && this.node.isValid) {
this.node.off(GameEvent.NewWave, this.onNewWave, this);
this.node.off(NodeEventType.TOUCH_END, this.onNodeClicked, this);
this.node.off(NodeEventType.TOUCH_START, this.onTouchStart, this);
this.node.off(NodeEventType.TOUCH_END, this.onTouchEnd, this);
this.node.off(NodeEventType.TOUCH_CANCEL, this.onTouchEnd, this);
}
// 清理长按计时器,防止销毁后回调访问失效节点
this.unschedule(this.showInfo);
oops.message.off(GameEvent.NewWave, this.onNewWaveGlobal, this);
// 通知 MissSkillsComp 回收该节点占用的槽位
oops.message.dispatchEvent(GameEvent.RemoveSkillBox, this.node);
}
private onNodeClicked() {
/** 长按时长(秒) */
private static readonly LONG_PRESS_TIME: number = 0.5;
/** 信息框是否已弹出(区分长按完成与短按取消) */
private info_shown: boolean = false;
/** 按下:启动长按计时 */
private onTouchStart() {
if (!this.initialized) return;
this.info_shown = false;
this.scheduleOnce(this.showInfo, SkillBoxComp.LONG_PRESS_TIME);
}
/** 放开/取消:已弹出则关闭信息框,未弹出则取消长按计时 */
private onTouchEnd() {
this.unschedule(this.showInfo);
if (this.info_shown) {
this.info_shown = false;
// oops.gui 移除信息框
oops.gui.remove(UIID.HInfo);
}
}
/** 长按到点:弹出信息框 */
private showInfo() {
if (!this.initialized) return;
this.info_shown = true;
oops.audio.playEffect("music/button");
// 点击时弹出 HInfoComp传入卡牌 UUID 和等级以启用预览模式
// 弹出 HInfoComp传入卡牌 UUID 和等级以启用预览模式
const config = SkillCardList.find(c => c.uuid === this.s_uuid || c.skill === this.s_uuid);
const cardUuid = config ? config.uuid : this.s_uuid;