From 600567732617a22ba52bbe7788b27ba017b17877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=94=E6=B0=91=E5=B0=8F=E9=95=87?= <262610965@qq.com> Date: Sat, 23 Nov 2024 19:15:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20ifNode=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E5=AD=98=E5=9C=A8=E8=8A=82=E7=82=B9=E5=88=99?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E7=9A=84=E4=BE=BF=E6=8D=B7=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E3=80=82=E5=BC=80=E5=8F=91=20nodes=20=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E6=9D=83=E9=99=90=E4=B8=BA=20protected=EF=BC=8C=E6=96=B9?= =?UTF-8?q?=E4=BE=BF=E5=AD=90=E7=B1=BB=E6=89=A9=E5=B1=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/module/common/GameComponent.ts | 38 ++++++++++++++++++++------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/assets/module/common/GameComponent.ts b/assets/module/common/GameComponent.ts index 857b364..3ed1350 100644 --- a/assets/module/common/GameComponent.ts +++ b/assets/module/common/GameComponent.ts @@ -30,7 +30,7 @@ interface ResRecord { resId?: number } -/** +/** * 游戏显示对象组件模板 * 1、当前对象加载的资源,会在对象释放时,自动释放引用的资源 * 2、当前对象支持启动游戏引擎提供的各种常用逻辑事件 @@ -63,8 +63,8 @@ export class GameComponent extends Component { this.event.off(event); } - /** - * 触发全局事件 + /** + * 触发全局事件 * @param event 事件名 * @param args 事件参数 */ @@ -76,7 +76,7 @@ export class GameComponent extends Component { //#region 预制节点管理 /** 摊平的节点集合(所有节点不能重名) */ - private nodes: Map = null!; + protected nodes: Map = null!; /** 通过节点名获取预制上的节点,整个预制不能有重名节点 */ getNode(name: string): Node | undefined { @@ -86,6 +86,26 @@ export class GameComponent extends Component { return undefined; } + /** + * 通过节点名查找节点,并执行相应回调 + * @param nodeName 节点名 + * @param callback 如果节点存在,则执行 callback + * @param notCallback 如果节点不存在,则执行 notCallback + * @example + * this.ifNode("yourNodeName", node => { + * // your biz code + * }); + */ + ifNode(nodeName: string, callback: (node: Node) => void, notCallback?: () => void) { + const theNode = this.getNode(nodeName); + if (theNode && callback) { + callback(theNode); + return; + } + + notCallback!(); + } + /** 平摊所有节点存到Map中通过get(name: string)方法获取 */ nodeTreeInfoLite() { this.nodes = new Map(); @@ -354,13 +374,13 @@ export class GameComponent extends Component { //#endregion //#region 游戏逻辑事件 - /** + /** * 批量设置当前界面按钮事件 * @param bindRootEvent 是否对预制根节点绑定触摸事件 * @example * 注:按钮节点Label1、Label2必须绑定UIButton等类型的按钮组件才会生效,方法名必须与节点名一致 * this.setButton(); - * + * * Label1(event: EventTouch) { console.log(event.target.name); } * Label2(event: EventTouch) { console.log(event.target.name); } */ @@ -400,12 +420,12 @@ export class GameComponent extends Component { }); } - /** - * 批量设置全局事件 + /** + * 批量设置全局事件 * @example * this.setEvent("onGlobal"); * this.dispatchEvent("onGlobal", "全局事件"); - * + * * onGlobal(event: string, args: any) { console.log(args) }; */ protected setEvent(...args: string[]) { -- Gitee