
在项目中,触摸是必不可少的,然而也需要经常屏蔽一些触摸,比如点击某个按钮d出一个界面,此时只有这个界面上的事件可以触发,而下层的触摸就要关闭。这里本人主要用到了单点触摸:
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)
通过这样写之后,在d出的界面上点击下层的按钮,就不会响应了。
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中最常用的就是第二种方式,容易 *** 作和控制。
总结以上是内存溢出为你收集整理的基本控件使用(三)(触摸,CallFunc,Schedule)全部内容,希望文章能够帮你解决基本控件使用(三)(触摸,CallFunc,Schedule)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)