基本控件使用(三)(触摸,CallFunc,Schedule)
在项目中,触摸是必不可少的,然而也需要经常屏蔽一些触摸,比如点击某个按钮弹出一个界面,此时只有这个界面上的事件可以触发,而下层的触摸就要关闭。这里本人主要用到了单点触摸: local function touchBega(touch,event) returntrue end local function touchMoved(touch,event) print(“touchMoved”) end local function touchEnded(touch,event) print(“touchEnded”) end local listener = cc.EventListenerTouchOneByOne:create() --单点触摸对象 listener:setSwallowTouches(true) --触摸消息不向下传递 listener:registerScriptHandler(touchBegan,cc.Handler.EVENT_TOUCH_BEGAN) listener:registerScriptHandler(touchMoved,cc.Handler.EVENT_TOUCH_MOVED) listener:registerScriptHandler(touchEnded,cc.Handler.EVENT_TOUCH_ENDED) local dispatcher =cc.Director:getInstance():getEventDispatcher() dispatcher:addEventListenerWithSceneGraphPriority(listener,layer) 通过这样写之后,在弹出的界面上点击下层的按钮,就不会响应了。 CallFunc: 在Lua中,动作回调只剩一个CallFunc(C++中是CallFunc,CallFuncN,CallFuncND),当然也可以传递参数和不传参数。其中,传递参数时,其参数必须是一个table。具体用法如下: local function callBackFunc1() print(“callBackFunc1called”) end local function callBackFunc2(pSender) print(“callBackFunc2called”) --其中pSender就是执行者 end local function callBackFunc3(pSender,tab) print(“callBackFunc3called”) print(tab[1],tab[2]) end local func1 = cc.CallFunc:create(callBackFunc1) local func2 = cc.CallFunc:create(callBackFunc2) local func3 = cc.CallFunc:create(callBackFunc3,{1,2}) backSprite:runAction(cc.Sequence:create(cc.MoveTo:create(1.0,cc.p(300,300)),func1)) backSprite:runAction(cc.Sequence:create(cc.MoveTo:create(1.0,func2)) backSprite:runAction(cc.Sequence:create(cc.MoveTo:create(1.0,func3)) 实际写法上也没区别,就是带参数和不带参数(但是参数一定是一个table) schedule: 在Lua中,定时器有两种方式,一种是根据帧率来刷新,一种是自定义时间间隔刷新。 先看第一种方式: local function update1(dt) print(dt) --0.17左右 end local scheduler =cc.Director:getInstance():getScheduler() scheduler:scheduleUpdateWithPriorityLua(update1,0) 其中第一个参数就是函数名,第二个参数代表优先级(0即可) 第二种形式: local updateID,time local scduduler = cc.Director:getIntance():getScheduler() local function update2(dt) time= time + dt iftime > 3 then scheduler:unscheduleScriptEntry(updateID) end end updateID = scheduler:scheduleScriptFunc(update2,0.5,false) 其中这种方式开启的定时器都有一个ID,要根据ID停止定时器。开启定时器时第一个参数是函数名,第二个参数是间隔时间,第三个参数是无限循环(如果设置为true的话,那么定时器就不执行了)。 目前,在cocos2dx-lua中最常用的就是第二种方式,容易操作和控制。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |