触摸吞噬传递问题

节点树如下:

Canvas
    |
    button
    |
    touchLayer
    |
    others

touchLayer大小跟canvas大小一样,在touchLayer中注册了 touch / mouse 事件, button按钮节点就收不到点击事件了,如何让事件穿透过去?让button也可以响应

1赞

cocos支持事件的监听和派发,包括自定义事件。文档里有:
http://www.cocos.com/docs/creator/scripting/events.html

这个文档我看了,我认为这个并不能解决我的问题,panda在其他用户问题中回答这个,
http://forum.cocos.com/t/creator/44064/2?u=toalias

按照panda说的,我在touchLayer注册了 touch/mouse事件用来检测一些东西 (所以这个节点监听的touch事件的不能被off的),touchLayer下面的节点(节点树的button, cocos creator ui 创建的button)就收不到消息了,之前2d-js 可以通过 touchhandler函数里面来控制返回值来决定是否继续传递。现在不能了,有没有其他方法实现此种需求。

是的,在 creator 中,只要你的上层节点事件监听器在生效状态,就无法穿透到同级的下层节点,只能由接收事件的节点向根节点方向冒泡传递。你的需求需要想办法临时暂停 touchLayer 的事件监听器

直接贴代码了
UnchokeInputEvents.ts

const { ccclass, menu } = cc._decorator;
/** 疏通输入事件,该组件要放在包含监听触摸事件组件的后面 */
@ccclass
@menu("i18n:MAIN_MENU.component.ui/Unchoke Input Events")
export default class UnchokeInputEvents extends cc.Component {
    /** 是否吞噬事件 */
    private _isSwallow: boolean = false;

    onEnable() {
        if (this.node._touchListener) {
            this.node._touchListener.setSwallowTouches(this._isSwallow);
        }
    }
}
13赞

亲测可以,666

威武,亲测可行

牛逼666

Nice,66666,简单粗暴!

mark下

mouse 事件不能用

cc.Class({
extends: cc.Component,

properties: {
},

onLoad() {
    if ((cc.sys.os === cc.sys.OS_ANDROID) || 
        (cc.sys.os === cc.sys.OS_IOS))
    {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchBg, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
    } else {
        this.node.on(cc.Node.EventType.MOUSE_DOWN, this.onMouse_Down, this);
        this.node.on(cc.Node.EventType.MOUSE_UP, this.onMouse_Up, this);
    }
    
    this.endCallBack = null;
},

// ==========================================================================================


// ==========================================================================================

onTouchBg(event) {
    cc.log("onTouchBg");
    this.node._touchListener.setSwallowTouches(false);
    return false;
},

onTouchEnd(event) {
    cc.log("onTouchEnd");
    
    if (this.endCallBack) {
        this.endCallBack()
    }
},

// ==========================================================================================
onMouse_Down(event) {
    cc.log("onMouse_Down");
    return false;
},

onMouse_Up(event) {
    cc.log("onMouse_Up");

    if (this.endCallBack) {
        this.endCallBack()
    }
},

});

试试把Button放在touchLayer里面,useCapture为false,即事件绑定在Propagate 阶段。
或者把touchLayer放在Button里面,useCapture为true。

禁用多点触摸,似乎就不起作用了

感谢回复,我今天也发现了这个问题,我看看引擎源码看看有没有解决的办法

请问,你这个鼠标能穿透吗

请问,鼠标事件你有做过穿透吗

最近也在研究 mouse穿透事件 有大神知道不