当前位置:网站首页>CocosCreator事件派發使用
CocosCreator事件派發使用
2022-07-04 17:30:00 【RemoteDev】
1. 事件派發事件機制: 基於事件冒泡,由最底層節點,層層向上派發
如當前節點A有1個字節點,有子節點B,而B節點又有子節點C
那麼事件派發方向為 C--->B--->A
子節點C派發事件XXX,其父節B先獲得事件XXX控制權,處理該事件,然後再派發給B的父節點A,也可不派發,直接調用
event.propagationStopped = true;
來停止事件傳遞.這樣A節點就不會收到XXX事件.
節點派發事件: this.node.dispatchEvent(這裏傳入自定義事件類)
自定義事件類參考下面代碼:
// Event 由 cc 模塊導入
import { Event } from 'cc';
//類繼承Event
class MyEvent extends Event {
//構造時傳入事件名與是否冒泡參數,最後一個參數是事件附加信息
constructor(name: string, bubbles?: boolean, detail?: any) {
super(name, bubbles);
this.detail = detail;
}
public detail: any = null; // 自定義的屬性
}
節點事件派發示例 : this.node.dispatchEvent(new MyEvent('XXX',true,'事件附加消息'));
2.派發事件處理: 直接調用on來處理
在派發事件節點的上層節點調用on('事件名',(事件對象)=>{});
派發事件處理示例: this.node.on('XXX',(Event:eventObj)=>{});
3. 一句話簡單理解事件派發: 子節點 dispatchEvent 父節點on
边栏推荐
- 容器环境minor gc异常频繁分析
- Visual studio 2019 (localdb) mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports 782
- ble HCI 流控机制
- 第十八届IET交直流输电国际会议(ACDC2022)于线上成功举办
- Display opencv drawn pictures on MFC picture control control
- How to implicitly pass values when transferring forms
- Yanwen logistics plans to be listed on Shenzhen Stock Exchange: it is mainly engaged in international express business, and its gross profit margin is far lower than the industry level
- Go language loop statement (under Lesson 10)
- C# 服务器日志模块
- 新的职业已经出现,怎么能够停滞不前 ,人社部公布建筑新职业
猜你喜欢
Smart Logistics Park supply chain management system solution: digital intelligent supply chain enables a new supply chain model for the logistics transportation industry
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
新的职业已经出现,怎么能够停滞不前 ,人社部公布建筑新职业
How to choose one plus 10 pro and iPhone 13?
Go development: how to use go singleton mode to ensure the security of high concurrency of streaming media?
Overflow: the combination of auto and Felx
【测试开发】软件测试——基础篇
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
Can you really use MySQL explain?
【华为HCIA持续更新】SDN与FVC
随机推荐
Internet addiction changes brain structure: language function is affected, making people unable to speak neatly
VSCode修改缩进不成功,一保存就缩进四个空格
Go micro tutorial - Chapter 2 go micro V3 using gin and etcd
GO开发:如何利用Go单例模式保障流媒体高并发的安全性?
tp配置多数据库
电子宠物小狗-内部结构是什么?
将Opencv绘制图片显示在MFC Picture Control控件上
Difference between redis' memory obsolescence strategy and expiration deletion strategy
MVC模式和三层架构
7 RSA密码体制
整理混乱的头文件,我用include what you use
Visual studio 2019 (localdb) mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports 782
Solution du système de gestion de la chaîne d'approvisionnement du parc logistique intelligent
La 18e Conférence internationale de l'IET sur le transport d'électricité en courant alternatif et en courant continu (acdc2022) s'est tenue avec succès en ligne.
Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see
Spark 中的 Rebalance 操作以及与Repartition操作的区别
Solution of commercial supply chain coordination system in the mineral industry: build a digital intelligent supply chain platform to ensure the safe supply of mineral resources
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
To sort out messy header files, I use include what you use
The 18th IET AC / DC transmission International Conference (acdc2022) was successfully held online