cocos2d-x v3.x Lua 中 [cc.Layer] 如何不让触摸事件向下转递

cocos2d-x 3.3

代码:

--比倍界面层
local BiBeiLayer = class("BiBeiLayer", function ()
    return cc.LayerColor:create(cc.c4b(0,0,0,125))
end)

--初始化
function BiBeiLayer:ctor()
    
    -- 创建一个事件监听器类型为 OneByOne 的单点触摸
    local  listenner = cc.EventListenerTouchOneByOne:create()
    
    -- ture 吞并触摸事件,不向下级传递事件;
    -- fasle 不会吞并触摸事件,会向下级传递事件;
    -- 设置是否吞没事件,在 onTouchBegan 方法返回 true 时吞没
    listenner:setSwallowTouches(true)
    
    -- 实现 onTouchBegan 事件回调函数
    listenner:registerScriptHandler(function(touch, event)
        local location = touch:getLocation()

        print("EVENT_TOUCH_BEGAN")
        return true
    end, cc.Handler.EVENT_TOUCH_BEGAN )
    
    -- 实现 onTouchMoved 事件回调函数
    listenner:registerScriptHandler(function(touch, event)
        local locationInNodeX = self:convertToNodeSpace(touch:getLocation()).x     

        print("EVENT_TOUCH_MOVED")
    end, cc.Handler.EVENT_TOUCH_MOVED )
    
    -- 实现 onTouchEnded 事件回调函数
    listenner:registerScriptHandler(function(touch, event)
        local locationInNodeX = self:convertToNodeSpace(touch:getLocation()).x

        print("EVENT_TOUCH_ENDED")
    end, cc.Handler.EVENT_TOUCH_ENDED )

    local eventDispatcher = self:getEventDispatcher()
    -- 添加监听器
    eventDispatcher:addEventListenerWithSceneGraphPriority(listenner, self)

end


你可能感兴趣的:(cocos2d-x v3.x Lua 中 [cc.Layer] 如何不让触摸事件向下转递)