红色和白色都有点击事件,也没添加button组件,红色在上面有反应了,白色没有,有办法让它穿透吗
楼上的都在扯淡,需要实现穿透的节点挂上这个组件就可以了
const { ccclass, property } = cc._decorator;
@ccclass
export class TransmitTouch extends cc.Component {
@property([cc.Component.EventHandler])
protected clickEvents: cc.Component.EventHandler[] = new Array();
onLoad(): void {
this.node.on(cc.Node.EventType.TOUCH_START, (event) => {
if (this.clickEvents == null) {
return;
}
cc.Component.EventHandler.emitEvents(this.clickEvents, event);
});
this.node._touchListener.setSwallowTouches(false);
}
}
12赞
就是同等级的
我们研究了源码也是用类似方法实现的点击事件穿透
关键还是这个方法 setSwallowTouches
学习了,mark
哇,兄弟,我想给你100个赞。找了我好久,终于找到正确的方法了
牛~原来就this.node._touchListener.setSwallowTouches(false);一句话的事
1赞
这种方法2.4已经不能用了,3.0官方说还会改版
2.4.3还能用呢
亲测2.4.3还能用
啊这,看来我是被假消息骗了
2.4.2版本ts会提示报错找不到_touchListener,但是亲测能有效,也不知道为什么
2.4.3使用正常啊
this.node._touchListener.setSwallowTouches(false);
1赞
2.3.4用不了
(this.node as any)._touchListener.setSwallowTouches(false);
我用了这个为什么还是无法穿透呢 我的版本是2.4.3
点击事件标记下
补充一下吧,看了源码后,你会发现
这里的_touchListener可能会报空的原因,是因为引擎并不会主动创建listener的实例,只有手动调用了on去注册监听事件,并且是相关监听器对应的事件(touch就绪要监听touchbegan,touchend等touch的系列事件中的一个。mouse,则是mouseup等mouse事件的一个)才会创建一个监听器的实例