Cocos2d-x Lua实现长按事件

前端之家收集整理的这篇文章主要介绍了Cocos2d-x Lua实现长按事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. local MainScene = class("MainScene",cc.load("mvc").ViewBase)
  2.  
  3. -- 弹出的层
  4. local InfoLayer = class('InfoLayer',function ()
  5. return cc.Layer:create()
  6. end)
  7.  
  8. function InfoLayer:ctor()
  9. self:setScale(0)
  10. local winSize = cc.Director:getInstance():getWinSize()
  11. local bg = cc.Sprite:create('HelloWorld.png')
  12. bg:setScale(0.8)
  13. bg:setPosition(winSize.width/2,winSize.height/2)
  14. self:addChild(bg)
  15.  
  16. self:enableNodeEvents()
  17. end
  18.  
  19. function InfoLayer:onEnter()
  20. self:runAction(cc.EaseBackOut:create(cc.ScaleTo:create(0.3,1)))
  21. end
  22.  
  23. function InfoLayer:onExit()
  24.  
  25. end
  26.  
  27. -- 实现长按事件的节点
  28. local touchNode = class('touchNode',function ()
  29. return cc.Node:create()
  30. end)
  31.  
  32. local longPressTime = 1.5
  33. local preAccTime = 0.5
  34. function touchNode:ctor(Img)
  35. local bg = ccui.ImageView:create(Img)
  36. self:addChild(bg)
  37. local listener = cc.EventListenerTouchOneByOne:create()
  38. listener:registerScriptHandler(function(touch,event)
  39. self.beganPoint = touch:getLocation()
  40. local point = bg:convertToNodeSpace(self.beganPoint)
  41. local rect = cc.rect(0,0,bg:getContentSize().width,bg:getContentSize().height)
  42. if cc.rectContainsPoint(rect,point) then
  43. self.tick = 0
  44. self.moved = false
  45. self.ended = false
  46. local seq = cc.Sequence:create(cc.CallFunc:create(function() self:accumate() end),cc.DelayTime:create(preAccTime))
  47. self.acc = self:runAction(cc.RepeatForever:create(seq))
  48. end
  49. return true
  50. end,cc.Handler.EVENT_TOUCH_BEGAN)
  51. listener:registerScriptHandler(function(touch,event)
  52. self.movedPoint = touch:getLocation()
  53. self.moved = true
  54. end,cc.Handler.EVENT_TOUCH_MOVED)
  55. listener:registerScriptHandler(function(touch,event)
  56. self:stopAction(self.acc)
  57. self.ended = true
  58. end,cc.Handler.EVENT_TOUCH_ENDED)
  59. self:getEventDispatcher():addEventListenerWithSceneGraPHPriority(listener,self)
  60. local function onNodeEvent(event)
  61. if event == 'exit' then
  62. self:getEventDispatcher():removeEventListener(listener)
  63. end
  64. end
  65. self:registerScriptHandler(onNodeEvent)
  66. end
  67.  
  68. function touchNode:accumate()
  69. self.tick = self.tick + preAccTime
  70. if self.tick > longPressTime - preAccTime / 2 and self.tick <= longPressTime + preAccTime / 2 then
  71. self:stopAction(self.acc)
  72. if not self.ended then
  73. ---------long press---------
  74. local function longPress()
  75. local layer = InfoLayer.new()
  76. cc.Director:getInstance():getRunningScene():addChild(layer)
  77. end
  78. if self.moved then
  79. if math.abs(self.beganPoint.x - self.movedPoint.x) < 10 and math.abs(self.beganPoint.y - self.movedPoint.y) < 10 then
  80. longPress()
  81. end
  82. else
  83. longPress()
  84. end
  85. end
  86. end
  87. end
  88.  
  89. function MainScene:ctor()
  90. local winSize = cc.Director:getInstance():getWinSize()
  91. local touchNode = touchNode.new('DS07.png')
  92. touchNode:setPosition(winSize.width/2,winSize.height/2)
  93. self:addChild(touchNode)
  94. end
  95.  
  96. return MainScene

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