当前位置: 首页 > news >正文

【cocos creator】【ts】事件派发系统

触发使用:
EventTool.emit(“onClick”)
需要监听的地方,onload调用:
EventTool.on(“onClick”, this.onClickEvent, this)


/**事件派发*/class EventTool {protected static _instance: EventTool = null;public static get Instance(): EventTool {if (EventTool._instance == null) {EventTool._instance = new EventTool();}return EventTool._instance;}isValidNode(node) {return node && node.isValid;}/**添加事件监听  * @param {string} event 事件名  * @param {Function} handler 事件响应函数  * @param {object} scope 函数所在的对象*/on(event: any, handler: Function, scope: any) {if (typeof handler != "function") {console.error("没有事件响应函数!", event);return;}let id = scope.uuid || scope.__custom_id__;if (!id) {scope.__custom_id__ = "" + Date.now() + Math.random();id = scope.__custom_id__;}this[id] = this[id] == undefined ? [] : this[id];if (Array.isArray(this[id])) {this[id].push(event);} else {console.error("有一个跟当前脚本同名的事件!");return;}this[event] = this[event] ? this[event] : {};this[event][id] = { handler: handler, scope: scope, times: cc.macro.REPEAT_FOREVER };}onOnce(event, handler, scope) {this.on(event, handler, scope)let id = scope.uuid || scope.__custom_id__;this[event][id].times = 1}/*** 移除事件监听* @param scope * @param event event 为空则移除scope所有监听*/off(scope?: any, event?: any) {let id = scope.uuid || scope.__custom_id__;if (!id) {return;}if (event) {this[event] = this[event] ? this[event] : {};this[event][id] = null;delete this[event][id];return;}let events = this[id];if (Array.isArray(events)) {for (let i = 0; i < events.length; i++) {this[events[i]][id] = null;delete this[events[i]][id];}}this[id] = [];delete this[id];}/**发送事件  * 参数个数不限,第一个参数为事件名,其余为传递给响应函数的参数  * @param {string} event 事件名    * @param args {}传递给响应函数的参数集,可以为空 */emit<T>(event, ...args) {let onwers = this[event];// console.log("trigger event:" + event);if (onwers) {for (let key in onwers) {let data = onwers[key];if (--data.times <= 0) {delete onwers[key];}try {if (!data.scope || !data.scope.node || !data.handler) continue;data.handler.apply(data.scope, args);} catch (e) {// console.error(e)}}} else {// console.log(event + "事件还没有添加事件监听!");}}
};
export default EventTool.Instance
http://www.lryc.cn/news/519903.html

相关文章:

  • 《探索鸿蒙Next上开发人工智能游戏应用的技术难点》
  • CSS | CSS实现两栏布局(左边定宽 右边自适应,左右成比自适应)
  • acwing_3195_有趣的数
  • Liunx-搭建安装VSOMEIP环境教程 执行 运行VSOMEIP示例demo
  • Git | git revert命令详解
  • ASP.NET Core 中,Cookie 认证在集群环境下的应用
  • Flyte工作流平台调研(五)——扩展集成
  • 【AUTOSAR 基础软件】软件组件的建立与使用(“代理”SWC)
  • java通过ocr实现识别pdf中的文字
  • Git 命令代码管理详解
  • Docker的安装和使用
  • Flink系统知识讲解之:Flink内存管理详解
  • 使用JMeter模拟多IP发送请求!
  • 【Ubuntu与Linux操作系统:六、软件包管理】
  • 【数据结构-堆】力扣1834. 单线程 CPU
  • 【前端动效】原生js实现拖拽排课效果
  • C#使用OpenTK绘制3D可拖动旋转图形三棱锥
  • 排序的本质、数据类型及算法选择
  • Python的列表基础知识点(超详细流程)
  • HarmonyOS鸿蒙开发 弹窗及加载中指示器HUD功能实现
  • 【Ubuntu与Linux操作系统:一、Ubuntu安装与基本使用】
  • React 元素渲染
  • 【2024年华为OD机试】 (C卷,100分)- 括号匹配(Java JS PythonC/C++)
  • 解锁企业数字化转型新力量:OpenCoze(开源扣子)
  • 【网络云SRE运维开发】2025第2周-每日【2025/01/12】小测-【第12章 rip路由协议】理论和实操考试题解析
  • 【微服务】8、分布式事务 ( XA 和 AT )
  • CVE-2025-22777 (CVSS 9.8):WordPress | GiveWP 插件的严重漏洞
  • TypeScript Jest 单元测试 搭建
  • 基于 SSH 的任务调度系统
  • filestream安装使用全套+filebeat的模块用法