加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 19:26:25 所属栏目:百科 来源:网络整理
导读:cocos2d-x 3.3 代码: --比倍界面层local BiBeiLayer = class("BiBeiLayer",function () return cc.LayerColor:create(cc.c4b(0,125))end)--初始化function BiBeiLayer:ctor() -- 创建一个事件监听器类型为 OneByOne 的单点触摸 local listenner = cc.EventL

cocos2d-x 3.3

代码:

--比倍界面层
local BiBeiLayer = class("BiBeiLayer",function ()
    return cc.LayerColor:create(cc.c4b(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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读