基本控件使用(三)(触摸,CallFunc,Schedule)
2016-07-26 20:15
555 查看
在项目中,触摸是必不可少的,然而也需要经常屏蔽一些触摸,比如点击某个按钮弹出一个界面,此时只有这个界面上的事件可以触发,而下层的触摸就要关闭。这里本人主要用到了单点触摸:
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,cc.p(300, 300)), func2))
backSprite:runAction(cc.Sequence:create(cc.MoveTo:create(1.0,cc.p(300, 300)), 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中最常用的就是第二种方式,容易操作和控制。
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,cc.p(300, 300)), func2))
backSprite:runAction(cc.Sequence:create(cc.MoveTo:create(1.0,cc.p(300, 300)), 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中最常用的就是第二种方式,容易操作和控制。
相关文章推荐
- 详解Lua中的表的概念及其相关操作方法
- Lua编程示例(二):面向对象、metatable对表进行扩展
- 把Lua编译进nginx步骤方法
- Lua脚本自动生成APK包
- Lua中的元表(metatable)、元方法(metamethod)详解
- Lua中的metatable介绍
- Lua中ipair和pair的区别
- Lua中的函数精讲笔记
- 浅谈Lua的面向对象特性
- 详解Lua中的变量相关知识点
- Lua脚本语言入门笔记
- Lua脚本调用外部脚本
- 详解Lua中的if语句的使用方法
- Lua中调用函数使用点号和冒号的区别
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中强大的元方法__index详解
- Lua中调用C++函数示例
- Lua面向对象之类和继承浅析
- Lua性能优化技巧(一):前言
- Lua中获取table长度问题探讨