fix: 修复英雄列表导航循环和节点查找逻辑
- 将前后导航改为循环切换,避免边界条件导致的无法切换 - 统一使用 findNodeByName 方法查找节点,解决大小写不一致导致的节点查找失败 - 添加 HeroList 空数组检查,防止运行时错误
This commit is contained in:
@@ -46,17 +46,15 @@ export class HListComp extends CCComp {
|
||||
}
|
||||
|
||||
private onPreClick() {
|
||||
if (this.currentIndex > 0) {
|
||||
this.currentIndex--;
|
||||
this.updateHeroView();
|
||||
}
|
||||
if (!HeroList || HeroList.length === 0) return;
|
||||
this.currentIndex = (this.currentIndex - 1 + HeroList.length) % HeroList.length;
|
||||
this.updateHeroView();
|
||||
}
|
||||
|
||||
private onNextClick() {
|
||||
if (this.currentIndex < HeroList.length - 1) {
|
||||
this.currentIndex++;
|
||||
this.updateHeroView();
|
||||
}
|
||||
if (!HeroList || HeroList.length === 0) return;
|
||||
this.currentIndex = (this.currentIndex + 1) % HeroList.length;
|
||||
this.updateHeroView();
|
||||
}
|
||||
|
||||
private updateHeroView() {
|
||||
@@ -123,13 +121,23 @@ export class HListComp extends CCComp {
|
||||
}
|
||||
}
|
||||
|
||||
private findNodeByName(root: Node, name: string): Node | null {
|
||||
if (!root) return null;
|
||||
if (root.name === name) return root;
|
||||
for (let i = 0; i < root.children.length; i++) {
|
||||
const res = this.findNodeByName(root.children[i], name);
|
||||
if (res) return res;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private updateSkillInfo(hero: any) {
|
||||
if (!this.info_node) return;
|
||||
|
||||
const skills = Object.values(hero.skills || {});
|
||||
|
||||
for (let i = 1; i <= 5; i++) {
|
||||
const line = this.info_node.getChildByName(`Line${i}`);
|
||||
let line = this.findNodeByName(this.info_node, `Line${i}`) || this.findNodeByName(this.info_node, `line${i}`);
|
||||
if (!line) continue;
|
||||
|
||||
const skill: any = skills[i - 1];
|
||||
@@ -140,7 +148,7 @@ export class HListComp extends CCComp {
|
||||
|
||||
const text = config ? `${config.name} Lv.${skill.lv} CD:${skill.cd}s ${config.info}` : `未知技能 CD:${skill.cd}s`;
|
||||
|
||||
const noteNode = line.getChildByName("note");
|
||||
const noteNode = this.findNodeByName(line, "note");
|
||||
const label = noteNode?.getComponent(Label) || noteNode?.getComponentInChildren(Label) || line.getComponentInChildren(Label);
|
||||
if (label) {
|
||||
label.string = text;
|
||||
@@ -154,9 +162,9 @@ export class HListComp extends CCComp {
|
||||
}
|
||||
|
||||
private updateLineTypeIcon(line: Node, iType?: IType) {
|
||||
const meleeNode = line.getChildByName("Melee");
|
||||
const remoteNode = line.getChildByName("remote");
|
||||
const supportNode = line.getChildByName("support");
|
||||
const meleeNode = this.findNodeByName(line, "Melee");
|
||||
const remoteNode = this.findNodeByName(line, "remote");
|
||||
const supportNode = this.findNodeByName(line, "support");
|
||||
if (meleeNode) meleeNode.active = iType === IType.Melee;
|
||||
if (remoteNode) remoteNode.active = iType === IType.remote;
|
||||
if (supportNode) supportNode.active = iType === IType.support;
|
||||
|
||||
Reference in New Issue
Block a user