Cocos2dx-Lua 长按手势

前端之家收集整理的这篇文章主要介绍了Cocos2dx-Lua 长按手势前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Cocos2dx只有touchUpInsid的按钮或菜单,有时可能会想要一个长按手势来触发事件。

-- 这是类名,命名这件事还是很麻烦的,凑合就是Button吧

local Button = {}

-- 触发时间,按多长时间触发

local mTriggerTime = 0.5
-- 可抖动区域,如果在长按触发前移动超过这个距离则不会触发事件
local removableDis = 10

-- 类方法sprite -- 给谁添加这个事件 longPressHandler -- 长按触发回调方法

clickHandler -- 还可以添加单击事件(touchIn)可设定触发时间 triggerTime

Button.registerLongPressHandler = function(sprite,longPressHandler,clickHandler,triggerTime)
triggerTime = triggerTime or mTriggerTime

-- 记录长按时间

local countTime = nil

-- 是用scheduler 来计时的,记录scheduler 的id

local sid = nil

-- 开始位置

local beganPoint= nil

-- 有没有回调过

local haveCall = false

-- 有没移动(超出可移动范围)

local isMoved = false
-- 有没已添加过监听
if sprite.m_listenner_initFlag then
return
end
-- 计时方法
local function countTimeFunc( delay )
countTime = countTime or 0
countTime = countTime + delay
if countTime > triggerTime then
-- 回调
if longPressHandler then
longPressHandler(sprite,"longPressBegin")
end

-- 停止计时
local scheduler = cc.Director:getInstance():getScheduler()
scheduler:unscheduleScriptEntry(sid)
sid = nil
countTime = nil
haveCall = true
end
end

-- 触摸开始

local function onTouchBegin(touch,event) Button.isCancel = false local UIUtil = require("ui/UIUtil") if UIUtil.isNodeVisible(sprite) and cc.rectContainsPoint(sprite:getBoundingBox(),sprite:getParent():convertTouchToNodeSpace(touch)) then beganPoint = touch:getLocation() -- 开始计时 if longPressHandler then local scheduler = cc.Director:getInstance():getScheduler() sid = scheduler:scheduleScriptFunc( function ( delay ) countTimeFunc(delay) end,0.1,false) end return true else return false end end -- 触摸移动 local function onTouchMoved( touch,event ) if haveCall == false and sid then local location = touch:getLocation() local width = location.x - beganPoint.x local height = location.y - beganPoint.y -- 停止计时 if math.abs(width) > removableDis or math.abs(height) > removableDis then local scheduler = cc.Director:getInstance():getScheduler() scheduler:unscheduleScriptEntry(sid) sid = nil countTime = nil isMoved = true end end end -- 触摸结束 local function onTouchEnded( touch,event ) if sid then local scheduler = cc.Director:getInstance():getScheduler() scheduler:unscheduleScriptEntry(sid) sid = nil end if not Button.isCancel then if haveCall then if longPressHandler then longPressHandler(sprite,"longPressEnded") end else if isMoved == false and clickHandler then clickHandler(sprite) end end end beganPoint=nil haveCall = false isMoved = false end function Button.cancel_long_press() onTouchEnded() Button.isCancel = true end -- 添加监听 local listenner = cc.EventListenerTouchOneByOne:create() listenner:setSwallowTouches(false) listenner:registerScriptHandler(onTouchBegin,cc.Handler.EVENT_TOUCH_BEGAN) listenner:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED) listenner:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED) local eventDispatcher = sprite:getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraPHPriority(listenner,sprite) sprite.m_listenner_initFlag = true return sprite end -- 取消事件 Button.isCancel = false return Button 原文链接:https://www.f2er.com/cocos2dx/344638.html

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