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

前端之家收集整理的这篇文章主要介绍了cocos2d-x v3.x Lua 中 [cc.Layer] 如何不让触摸事件向下转递前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

cocos2d-x 3.3

代码

  1. --比倍界面层
  2. local BiBeiLayer = class("BiBeiLayer",function ()
  3. return cc.LayerColor:create(cc.c4b(0,125))
  4. end)
  5.  
  6. --初始化
  7. function BiBeiLayer:ctor()
  8. -- 创建一个事件监听器类型为 OneByOne 的单点触摸
  9. local listenner = cc.EventListenerTouchOneByOne:create()
  10. -- ture 吞并触摸事件,不向下级传递事件;
  11. -- fasle 不会吞并触摸事件,会向下级传递事件;
  12. -- 设置是否吞没事件,在 onTouchBegan 方法返回 true 时吞没
  13. listenner:setSwallowTouches(true)
  14. -- 实现 onTouchBegan 事件回调函数
  15. listenner:registerScriptHandler(function(touch,event)
  16. local location = touch:getLocation()
  17.  
  18. print("EVENT_TOUCH_BEGAN")
  19. return true
  20. end,cc.Handler.EVENT_TOUCH_BEGAN )
  21. -- 实现 onTouchMoved 事件回调函数
  22. listenner:registerScriptHandler(function(touch,event)
  23. local locationInNodeX = self:convertToNodeSpace(touch:getLocation()).x
  24.  
  25. print("EVENT_TOUCH_MOVED")
  26. end,cc.Handler.EVENT_TOUCH_MOVED )
  27. -- 实现 onTouchEnded 事件回调函数
  28. listenner:registerScriptHandler(function(touch,event)
  29. local locationInNodeX = self:convertToNodeSpace(touch:getLocation()).x
  30.  
  31. print("EVENT_TOUCH_ENDED")
  32. end,cc.Handler.EVENT_TOUCH_ENDED )
  33.  
  34. local eventDispatcher = self:getEventDispatcher()
  35. -- 添加监听器
  36. eventDispatcher:addEventListenerWithSceneGraPHPriority(listenner,self)
  37.  
  38. end

猜你在找的Cocos2d-x相关文章