当前位置:网站首页>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
边栏推荐
- 照明行业S2B2B解决方案:高效赋能产业供应链,提升企业经济效益
- 第十八届IET交直流輸電國際會議(ACDC2022)於線上成功舉辦
- It's too convenient. You can complete the code release and approval by nailing it!
- Ble HCI flow control mechanism
- VB cannot access database stocks
- R语言plotly可视化:plotly可视化互相重叠的直方图(historgram)、并在直方图的顶部边缘使用geom_rug函数添加边缘轴须图Marginal rug plots
- 公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
- R语言plotly可视化:plotly可视化多分类变量小提琴图(multiple variable violin plot in R with plotly)
- Firewall basic transparent mode deployment and dual machine hot standby
- wuzhicms代码审计
猜你喜欢

"Cannot initialize Photoshop because the temporary storage disk is full" graphic solution

The Ministry of human resources and Social Security announced the new construction occupation

Go micro tutorial - Chapter 2 go micro V3 using gin and etcd

智慧物流園區供應鏈管理系統解决方案:數智化供應鏈賦能物流運輸行業供應鏈新模式

新的职业已经出现,怎么能够停滞不前 ,人社部公布建筑新职业

开发者,MySQL专栏完更,助你轻松从安装到入门进阶

Zhijieyun - meta universe comprehensive solution service provider

【华为HCIA持续更新】SDN与FVC

居家打工年入800多万,一共五份全职工作,他还有时间打游戏

Congratulations to Mr. Zhang Pengfei, chief data scientist of artefact, for winning the campaign Asia tech MVP 2022
随机推荐
To sort out messy header files, I use include what you use
Summary of tx.origin security issues
R语言plotly可视化:plotly可视化互相重叠的直方图(historgram)、并在直方图的顶部边缘使用geom_rug函数添加边缘轴须图Marginal rug plots
【测试开发】软件测试——基础篇
新的职业已经出现,怎么能够停滞不前 ,人社部公布建筑新职业
解读数据安全治理能力评估框架2.0,第四批DSG评估征集中
建筑建材行业经销商协同系统解决方案:赋能企业构建核心竞争力
CocosCreator事件派发使用
智慧物流園區供應鏈管理系統解决方案:數智化供應鏈賦能物流運輸行業供應鏈新模式
Kunming Third Ring Road Closure project will pass through these places. Is there one near your home?
离线、开源版的 Notion—— 笔记软件Anytype 综合评测
Zebras are recognized as dogs, and the reason for AI's mistakes is found by Stanford
detectron2安装方法
To sort out messy header files, I use include what you use
容器环境minor gc异常频繁分析
leetcode:421. The maximum XOR value of two numbers in the array
【模板】【luogu P4630】Duathlon 铁人两项(圆方树)
[template] [Luogu p4630] duathlon Triathlon (round square tree)
超大规模数仓集群在大型商业银行的落地实践
PyTorch深度学习快速入门教程