cocos2d-x button 怎么监听按住不放事件
展开全部
看下面的代码,这个代码实现了长按按钮、单机、双击、三机的事件处理
local GameScene = class("GameScene",function()
return
cc.Scene:create()
end)
function GameScene.create()
local scene
= GameScene.new()
scene:initEvent()
return
scene
end
function GameScene:initEvent()
local
size = cc.Director:getInstance():getVisibleSize()
local orign
= cc.Director:getInstance():getVisibleOrigin()
local btn
= ccui.Button:create("menu1.png")
btn:setPosition(cc.p(orign.x + size.width/2, orign.y +
size.height/2))
btn:setAnchorPoint(cc.p(0.5,0.5))
self:addChild(btn)
btn:setScale(0.5)
local
function beginhandle()
if self.isTouch then
self.count = self.count + 1
if self.count >= 2 then
self.longPress = true
self.count = 0
print("long press handle")
end
end
end
local
function singleClick()
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.oneHandle)
self.count = 0
print("single click handle")
end
local
function doubleClick()
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.twoHandle)
self.count = 0
print("double click handle")
end
local
function threeClick()
self.count = 0
print("tree click handle")
end
local
function eventTouch(ref, type)
if ref == btn then
if type == ccui.TouchEventType.began then
self.isTouch = true
self.beginHandle =
cc.Director:getInstance():getScheduler():scheduleScriptFunc(beginhandle,1,false)
elseif type == ccui.TouchEventType.moved then
self.isMoved = true
elseif type == ccui.TouchEventType.ended then
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.beginHandle)
self.isTouch = false
if self.longPress then
self.longPress = false
self.count = 0
return false
end
if self.isMoved then
self.isMoved = false
return false
end
if self.count == 2 then
threeClick()
self.count = 0
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.oneHandle)
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.twoHandle)
elseif self.count == 1 then
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.oneHandle)
self.twoHandle =
cc.Director:getInstance():getScheduler():scheduleScriptFunc(doubleClick,0.25,false)
self.count = self.count + 1
elseif self.count == 0 then
self.oneHandle =
cc.Director:getInstance():getScheduler():scheduleScriptFunc(singleClick,0.25,false)
self.count = self.count + 1
end
end
end
end
btn:addTouchEventListener(eventTouch)
end
function GameScene:ctor()
self.visibleSize = cc.Director:getInstance():getVisibleSize()
self.origin
= cc.Director:getInstance():getVisibleOrigin()
self.schedulerID = nil
self.count =
0
self.isTouch
= false
self.longPress = false
self.isMoved
= false
end
function GameScene:playBgMusic()
end
return GameScene
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询