当前位置:网站首页>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
边栏推荐
- CANN算子:利用迭代器高效实现Tensor数据切割分块处理
- Readis configuration and optimization of NoSQL (final chapter)
- KS007基于JSP实现人个人博客系统
- 网页游戏引擎
- PingCode 性能测试之负载测试实践
- Congratulations to Mr. Zhang Pengfei, chief data scientist of artefact, for winning the campaign Asia tech MVP 2022
- egg. JS learning notes
- Overflow: the combination of auto and Felx
- 照明行业S2B2B解决方案:高效赋能产业供应链,提升企业经济效益
- NFT liquidity market security issues occur frequently - Analysis of the black incident of NFT trading platform quixotic
猜你喜欢
To sort out messy header files, I use include what you use
NFT liquidity market security issues occur frequently - Analysis of the black incident of NFT trading platform quixotic
Redis 的内存淘汰策略和过期删除策略的区别
To sort out messy header files, I use include what you use
Congratulations to Mr. Zhang Pengfei, chief data scientist of artefact, for winning the campaign Asia tech MVP 2022
Overflow: the combination of auto and Felx
什么是低代码开发?
leetcode:421. 数组中两个数的最大异或值
Can you really use MySQL explain?
Learn more about the basic situation of 2022pmp examination
随机推荐
Go语言循环语句(第10课下)
智慧物流园区供应链管理系统解决方案:数智化供应链赋能物流运输行业供应链新模式
[glide] cache implementation - memory and disk cache
Is it safe for CITIC Securities to open an account online? Is the account opening fee charged
KS007基于JSP实现人个人博客系统
Height residual method
tx.origin安全问题总结
What is low code development?
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
安信证券属于什么档次 开户安全吗
防火墙基础透明模式部署和双机热备
Array filter fliter in JS
S2b2b solution for lighting industry: efficiently enable the industrial supply chain and improve the economic benefits of enterprises
PingCode 性能测试之负载测试实践
MVC模式和三层架构
C# 更加优质的操作MongoDB数据库
【云原生】服务网格是什么“格”?
Embedded software architecture design - function call
Is it safe for Great Wall Securities to open an account? How to open a securities account
Capvision Rongying's prospectus in Hong Kong was "invalid": it was strictly questioned by the CSRC and required supplementary disclosure