使用dispatchEvent派发可以冒泡的形式一层一层向父物体传递事件。
//Label const {ccclass, property} = cc._decorator; @ccclass export default class Helloworld extends cc.Component { start() { this.node.on("say",function(){ console.log(this.node.name + ":Hello") }.bind(this)) //派送自定义事件 是否冒泡传递 this.node.dispatchEvent(new cc.Event.EventCustom("say",true)) } }
//Background、Btn、Canvas const {ccclass, property} = cc._decorator; @ccclass export default class LoadTest extends cc.Component { start () { this.node.on("say",function(event){ console.log(this.node.name + ":Hello") if(this.node.name == "Btn") { event.stopPropagation() //停止向上派送事件 } }.bind(this)) } onDestroy() { //关闭监听事件 this.node.off("say") } }