首先对按钮注册监听
btnSkill:addTouchEventListener(eventTouch) -- eventTouch为回调函数
然后
local function eventTouch(ref, type)
if ref == btnSkill then
if type == ccui.TouchEventType.began then --开始触摸的时候
isTouch = true --设置一个触摸标签
--创建一个定时器每0.5秒调用一次beginHandle这个函数,返回一个Id,用于unscheduleScriptFunc( Id)
self._beginHandle = cc.Director:getInstance():getScheduler():scheduleScriptFunc(beginhandle,0.5 ,false)
elseif type == ccui.TouchEventType.moved then --触摸移动的时候
--
elseif type == ccui.TouchEventType.ended then --触摸结束的时候
--取消创建的定时器
cc.Director:getInstance():getScheduler():unscheduleScriptEntry( self._beginHandle)
isTouch = false
self._skillNode:setVisible(false )
end
end
end
然后实现定时器中的回调函数beginhandle
local function beginhandle()
if isTouch then --判断是否被触摸
count = count + 1 --开始计时
if count >= 2 then
count = 0
self._skillNode:setVisible(true ) --函数的实现
self:showSkill(v)
end
end
end
由于声明先后的原因,正确顺序应该是:
local count = 0
local isTouch = false
local function beginhandle()
if isTouch then --判断是否被触摸
count = count + 1 --开始计时
if count >= 2 then
count = 0
self._skillNode:setVisible(true ) --函数的实现
self:showSkill(v)
end
end
end
local function eventTouch(ref, type)
if ref == btnSkill then
if type == ccui.TouchEventType.began then --开始触摸的时候
isTouch = true --设置一个触摸标签
--创建一个定时器每0.5秒调用一次beginHandle这个函数,返回一个Id,用于unscheduleScriptFunc( Id)
self._beginHandle = cc.Director:getInstance():getScheduler():scheduleScriptFunc(beginhandle,0.5 ,false)
elseif type == ccui.TouchEventType.moved then --触摸移动的时候
--
elseif type == ccui.TouchEventType.ended then --触摸结束的时候
--取消创建的定时器
cc.Director:getInstance():getScheduler():unscheduleScriptEntry( self._beginHandle)
isTouch = false
self._skillNode:setVisible(false )
end
end
end
btnSkill:addTouchEventListener(eventTouch) -- eventTouch为回调函数
按钮长按功能
最新推荐文章于 2024-08-21 09:22:14 发布