cocos2d-x button 怎么监听按住不放事件

 我来答
就烦条0o
2016-01-21 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46492
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部

看下面的代码,这个代码实现了长按按钮、单机、双击、三机的事件处理

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
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式