当前位置:网站首页>CocosCreator事件派发使用
CocosCreator事件派发使用
2022-07-04 15:39: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
边栏推荐
- 网页游戏引擎
- Vb无法访问数据库stocks
- Median and order statistics
- C# 更加优质的操作MongoDB数据库
- It's too convenient. You can complete the code release and approval by nailing it!
- VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
- 超大规模数仓集群在大型商业银行的落地实践
- 中银证券网上开户安全吗?
- R language plot visualization: plot visualizes overlapping histograms and uses geom at the top edge of the histogram_ The rug function adds marginal rug plots
- What grade does Anxin securities belong to? Is it safe to open an account
猜你喜欢

Analysis of abnormal frequency of minor GC in container environment

CANN算子:利用迭代器高效实现Tensor数据切割分块处理

利用win10计划任务程序定时自动运行jar包

Embedded software architecture design - function call

Visual Studio 2019 (LocalDB)MSSQLLocalDB SQL Server 2014 数据库版本为852无法打开,此服务器支持782
Why do you say that the maximum single table of MySQL database is 20million? Based on what?

detectron2安装方法

C# 更加优质的操作MongoDB数据库

Ble HCI flow control mechanism

世界环境日 | 周大福用心服务推动减碳环保
随机推荐
Smart Logistics Park supply chain management system solution: digital intelligent supply chain enables a new supply chain model for the logistics transportation industry
照明行业S2B2B解决方案:高效赋能产业供应链,提升企业经济效益
Which domestic cloud management platform manufacturer is good in 2022? Why?
With an annual income of more than 8 million, he has five full-time jobs. He still has time to play games
egg. JS learning notes
Kunming Third Ring Road Closure project will pass through these places. Is there one near your home?
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
第十八届IET交直流输电国际会议(ACDC2022)于线上成功举办
It's too convenient. You can complete the code release and approval by nailing it!
新享科技发布小程序UniPro小优 满足客户移动办公场景
Object. Usage of keys()
TP configuring multiple databases
Go micro tutorial - Chapter 2 go micro V3 using gin and etcd
手里10万元存款买什么理财产品收益最高?
Is it safe for Bank of China Securities to open an account online?
智慧物流園區供應鏈管理系統解决方案:數智化供應鏈賦能物流運輸行業供應鏈新模式
[acwing] 58 weeks 4490 dyeing
防火墙基础透明模式部署和双机热备
Zhijieyun - meta universe comprehensive solution service provider
Congratulations to Mr. Zhang Pengfei, chief data scientist of artefact, for winning the campaign Asia tech MVP 2022