您的位置:首页 > 移动开发 > Cocos引擎

Cocos之_从C++过渡到Lua

2016-02-29 16:25 441 查看
总结的非常好,都是我们学cocos2d-x以来摸索过的东西,如果早有这篇文章就能少走不少弯路了,特此截屏保存。

原文链接:http://shahdza.blog.51cto.com/2410787/1569003

【唠叨】

现在开始学习Lua了,开始用 Cocos Code IDE 开发cocos2dx的游戏了。

可是呢,因为 cocos2dx v3.x 刚刚才出来不久,所以网上的教程大部分都是基于C++版本的,

而针对Lua版本的大部分都是老版本 v2.x 的教程,对于用 v3.x 的我来说,很多都不适用了。

无奈之下只好自己摸索,经过几天的学习,积累了一些 Lua 与 C++ 开发cocos2dx游戏之间的差异。

现在就在此总结一下。

【番外】

Cocos2d-X官方就在2014年10月28日,发布了 Cocos引擎v1.0 Preview版 。

将Cocos Studio、Cocos Code IDE、Cocos2d-X 三件套进行了集成,一键部署安装。

看着实在是太酷炫了,这也说明使用脚本语言 Lua 开发cocos2dx游戏必将越发流行。

【从C++过渡到Lua】

1、Lua中如何使用cocos2dx引擎中的类?

在C++中是这样调用的:
<code>    Sprite* sprite = Sprite::create();
sprite->setPosition(Vec2(<span class="hljs-number" style="color: rgb(0, 102, 102);">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">100</span>));
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>->addChild(sprite);
</code>


而在Lua中是这样调用的:
<code>    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">local</span> sprite = cc.Sprite:create()
sprite:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">set</span>Position(cc.p(<span class="hljs-number" style="color: rgb(0, 102, 102);">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">100</span>))
self:addChild(sprite)
</code>


是不是很简单?差别其实不是很大嘛!

Lua中使用引擎中的类,只要在前面多加个 cc. 即可。

而调用类的函数,不是双冒号:: ,而是一个冒号: 。

2、Lua 与 C++ 使用上有何差异?

请耐心看完以下这个类,你或许会有基本上的了解。

PS:我比较懒,所以用代码来代替文字描述了。
<code>local winSize = cc.Director:getInstance():getWinSize()

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 继承Layer类</span>
MenuLayer = class("MenuLayer", function()
return cc.Layer:<span class="hljs-operator"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">create</span>()
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>)

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 初始化函数</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> MenuLayer:ctor()
self.name = <span class="hljs-string" style="color: rgb(0, 136, 0);">"hello"</span>         <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 成员变量</span>
self.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">size</span> = cc.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">size</span>(<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>)   <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 成员变量</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 创建包含GameLayer的场景</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> MenuLayer:createScene()
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> scene = cc.Scene:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">create</span>()
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> layer = MenuLayer:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">create</span>()
scene:addChild(layer)
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> scene
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 创建GameLayer层</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> MenuLayer:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">create</span>()
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> layer = MenuLayer.new()  <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- new()</span>
layer:init()                   <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- init()</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> layer
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 初始化</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> MenuLayer:init()
self:ShowUI()       <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 添加界面元素(Sprite、Label等)</span>
self:addBtn()       <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 添加菜单按钮</span>
self:addTouches()   <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 添加多点触摸</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 添加界面</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> MenuLayer:ShowUI()

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 背景图片Sprite</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> bg = cc.Sprite:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">create</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">"HelloWorld.png"</span>)
bg:setPosition(cc.p(<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>))      <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 设置位置</span>
bg:setAnchorPoint(<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>)         <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 设置锚点</span>
self:addChild(bg)               <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 添加子节点</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 添加文字</span>
self.label = cc.Label:createWithSystemFont(<span class="hljs-string" style="color: rgb(0, 136, 0);">"debug"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0);">"res/fonts/Marker Felt.ttf"</span>,<span class="hljs-number" style="color: rgb(0, 102, 102);">30</span>)
self.label:setPosition(winSize.width/<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>, winSize.height/<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)
self:addChild(self.label)

<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 添加按钮</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> MenuLayer:addBtn()

<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> menu
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> normal, hard

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 回调函数</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- tag 为menuItem设置的标签setTag ,menuItem为相应对象</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> menuCallback(tag, menuItem)
print(<span class="hljs-string" style="color: rgb(0, 136, 0);">"menuItem: "</span> .. tag)    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- Lua中的输出语句</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

normal = cc.MenuItemImage:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">create</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">"normal.png"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"normal.png"</span>)
normal:setPosition(<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">120</span>)
normal:setTag(<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>)
normal:registerScriptTapHandler(menuCallback)   <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 按钮事件</span>

hard = cc.MenuItemImage:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">create</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">"hard.png"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"hard.png"</span>)
hard:setPosition(<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">20</span>)
hard:setTag(<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)
hard:registerScriptTapHandler(menuCallback)     <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 按钮事件</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 创建菜单,最后不需要加NULL</span>
menu = cc.Menu:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">create</span>(normal, hard)
self:addChild(menu)

<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 多点触摸</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> MenuLayer:addTouches()

<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> touch1, touch2 = cc.Touch, cc.Touch
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> onTouchesBegan(touches, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">event</span>)
print(<span class="hljs-string" style="color: rgb(0, 136, 0);">"Touches Began"</span>)
touch1 = touches[<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>]     <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 第一个触点,下标从1开始</span>
touch2 = touches[<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>]     <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 第二个触点</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> pos1 = touch1:getLocation()   <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 获取触点1的位置</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> pos2 = touch2:getLocation()   <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 获取触点2的位置</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> delta = {
x = pos2.x - pos1.x ,
y = pos2.y - pos1.y
}
print(delta.x .. <span class="hljs-string" style="color: rgb(0, 136, 0);">" , "</span> .. delta.y)  <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 输出log</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> onTouchesMoved(touches, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">event</span>)
print(<span class="hljs-string" style="color: rgb(0, 136, 0);">"Touches Moved"</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> onTouchesEnded(touches, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">event</span>)
print(<span class="hljs-string" style="color: rgb(0, 136, 0);">"Touches Ended"</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 注册多点触摸</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> dispatcher = cc.Director:getInstance():getEventDispatcher()
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> listener = cc.EventListenerTouchAllAtOnce:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">create</span>()
listener:registerScriptHandler(onTouchesBegan, cc.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Handler</span>.EVENT_TOUCHES_BEGAN)
listener:registerScriptHandler(onTouchesMoved, cc.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Handler</span>.EVENT_TOUCHES_MOVED)
listener:registerScriptHandler(onTouchesEnded, cc.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Handler</span>.EVENT_TOUCHES_ENDED)
dispatcher:addEventListenerWithSceneGraphPriority(listener, self)

<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>
</span></code>


3、Lua中的字符串格式转换?

C++中是这样使用的:
<code>    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">sprintf</span>(str, <span class="hljs-string" style="color: rgb(0, 136, 0);">"hero_<span class="hljs-variable" style="color: rgb(102, 0, 102);">%02d</span>.png"</span>, i)
</code>


而在Lua中则是使用 string.format() 函数。
<code>    string.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">format</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">"hero_<span class="hljs-variable" style="color: rgb(102, 0, 102);">%02d</span>.png"</span>, i)
</code>


4、Lua中的枚举类型

我们都知道C++中的枚举都改成了强枚举类型,形如 Control::State::NORMAL 。

而在Lua中将其变成形如:cc.CONTROL_STATE_NORMAL 。

估计你已经明白了吧?把所有字母都变成大写,然后把双冒号:: 变成下划线 _ 即可。

下面列出一些我自己整理的常见枚举类型在Lua中是如何使用的。
<code>    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- ResolutionPolicy 屏幕适配(就这个比较奇葩。。。)</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 含义参见:http://shahdza.blog.51cto.com/2410787/1550089</span>
cc.ResolutionPolicy.EXACT_FIT
cc.ResolutionPolicy.FIXED_HEIGHT
cc.ResolutionPolicy.FIXED_WIDTH
cc.ResolutionPolicy.NO_BORDER
cc.ResolutionPolicy.SHOW_ALL

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- EventKeyboard::KeyCode 键盘按键枚举类型(这个也比较奇葩。。。)</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 含义参见(键盘事件部分):http://shahdza.blog.51cto.com/2410787/1560222</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 键盘按键比较多,所以就罗列一部分</span>
cc.KeyCode.KEY_A
cc.KeyCode.KEY_1
cc.KeyCode.KEY_F1
cc.KeyCode.KEY_SPACE
cc.KeyCode.KEY_ALT
cc.KeyCode.KEY_SHIFT

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- Control::EventType 控件事件类型</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 含义参见:http://shahdza.blog.51cto.com/2410787/1543349</span>
cc.CONTROL_EVENTTYPE_TOUCH_DOWN
cc.CONTROL_EVENTTYPE_DRAG_INSIDE
cc.CONTROL_EVENTTYPE_DRAG_OUTSIDE
cc.CONTROL_EVENTTYPE_DRAG_ENTER
cc.CONTROL_EVENTTYPE_DRAG_EXIT
cc.CONTROL_EVENTTYPE_TOUCH_UP_INSIDE
cc.CONTROL_EVENTTYPE_TOUCH_UP_OUTSIDE
cc.CONTROL_EVENTTYPE_TOUCH_CANCEL
cc.CONTROL_EVENTTYPE_VALUE_CHANGED

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- Control::State 控件状态</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 含义参见:http://shahdza.blog.51cto.com/2410787/1543349</span>
cc.CONTROL_STATE_NORMAL
cc.CONTROL_STATE_DISABLED
cc.CONTROL_STATE_SELECTED
cc.CONTROL_STATE_HIGH_LIGHTED

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- EditBox::EditBoxInputMode 文本框虚拟键盘的编辑类型</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 含义参见:http://shahdza.blog.51cto.com/2410787/1544213</span>
cc.EDITBOX_INPUT_MODE_ANY
cc.EDITBOX_INPUT_MODE_URL
cc.EDITBOX_INPUT_MODE_DECIMAL
cc.EDITBOX_INPUT_MODE_NUMERIC
cc.EDITBOX_INPUT_MODE_EMAILADDR
cc.EDITBOX_INPUT_MODE_SINGLELINE
cc.EDITBOX_INPUT_MODE_PHONENUMBER

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- EditBox::EditBoxInputFlag 文本框文本类型</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 含义参见:http://shahdza.blog.51cto.com/2410787/1544213</span>
cc.EDITBOX_INPUT_FLAG_PASSWORD
cc.EDITBOX_INPUT_FLAG_SENSITIVE
cc.EDITBOX_INPUT_FLAG_INITIAL_CAPS_WORD
cc.EDITBOX_INPUT_FLAG_INITIAL_CAPS_SENTENCE
cc.EDITBOX_INPUT_FLAG_INITIAL_CAPS_ALL_CHARACTERS

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- EditBox::KeyboardReturnType 文本框虚拟键盘中return键显示字符</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 含义参见:http://shahdza.blog.51cto.com/2410787/1544213</span>
cc.KEYBOARD_RETURNTYPE_GO
cc.KEYBOARD_RETURNTYPE_DONE
cc.KEYBOARD_RETURNTYPE_SEND
cc.KEYBOARD_RETURNTYPE_SEARCH
cc.KEYBOARD_RETURNTYPE_DEFAULT

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- ScrollView::Direction 滚动方向</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 含义参见:http://shahdza.blog.51cto.com/2410787/1544983</span>
cc.SCROLLVIEW_DIRECTION_BOTH
cc.SCROLLVIEW_DIRECTION_VERTICAL
cc.SCROLLVIEW_DIRECTION_HORIZONTAL

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- TableView::VerticalFillOrder 列表视图排列方式</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 含义参见:http://shahdza.blog.51cto.com/2410787/1545383</span>
cc.TABLEVIEW_FILL_TOPDOWN
cc.TABLEVIEW_FILL_BOTTOMUP

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- ProgressTimer::Type</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 含义参见:http://shahdza.blog.51cto.com/2410787/1546707</span>
cc.PROGRESS_TIMER_TYPE_BAR
cc.PROGRESS_TIMER_TYPE_RADIAL

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- ParticleSystem::PositionType 粒子位置模式</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 含义参见:</span>
cc.POSITION_TYPE_FREE
cc.POSITION_TYPE_GROUPED
cc.POSITION_TYPE_RELATIVE

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- ParticleSystem::Mode 粒子发射器类型</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 含义参见:http://shahdza.blog.51cto.com/2410787/1547636</span>
cc.PARTICLE_MODE_RADIUS
cc.PARTICLE_MODE_GRAVITY

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- TransitionScene::Orientation 场景切换方向</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 含义参见:http://shahdza.blog.51cto.com/2410787/1547977</span>
cc.TRANSITION_ORIENTATION_UP_OVER
cc.TRANSITION_ORIENTATION_DOWN_OVER
cc.TRANSITION_ORIENTATION_LEFT_OVER
cc.TRANSITION_ORIENTATION_RIGHT_OVER

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- TextVAlignment 文本的垂直对其方式</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 含义参见:http://shahdza.blog.51cto.com/2410787/1560612</span>
cc.VERTICAL_TEXT_ALIGNMENT_TOP
cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM
cc.VERTICAL_TEXT_ALIGNMENT_CENTER

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- TextHAlignment 文本的水平对其方式</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 含义参见:http://shahdza.blog.51cto.com/2410787/1560612</span>
cc.TEXT_ALIGNMENT_LEFT
cc.TEXT_ALIGNMENT_RIGHT
cc.TEXT_ALIGNMENT_CENTER
</code>


5、Lua中的事件回调

cocos2dx中常用的事件回调有如下:

动作回调 : CallFunc

定时器刷新 : schedule

菜单项事件回调 : menuItem

按钮控件事件回调 : ControlButton

在cocos2dx v3.2中,由于支持了C++ 11,使用 std::bind 集成了 CC_CALLBACK_* 。

在Lua中可不能这样方便的使用。

5.1、动作回调 CallFunc

在C++中,动作回调参见:http://shahdza.blog.51cto.com/2410787/1553051

在Lua中只剩下一个 CallFunc ,其定义如下:

至于回调函数中有什么参数,参见下面的例子。
<code>    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- hander : 执行的回调函数</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- value  : 传递给回调函数的参数,必须为一个table</span>
cc.CallFunc:<span class="hljs-operator"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">create</span>(hander, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">value</span>)
</span></code>
<code>使用举例:
</code>
<code>    -- node : 执行动作的对象
-- tab  : 传过来的参数, 必须为一个table
local <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> <span class="hljs-title">callbackFunc</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(node, tab)</span>
<span class="hljs-title">node</span>:<span class="hljs-title">setScale</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)</span>
<span class="hljs-title">print</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-string" style="color: rgb(0, 136, 0);">"x="</span> .. tab.x .. <span class="hljs-string" style="color: rgb(0, 136, 0);">",y="</span> .. tab.y)</span>
<span class="hljs-title">end</span>

<span class="hljs-title">local</span> <span class="hljs-title">sprite</span> = <span class="hljs-title">cc</span>.<span class="hljs-title">Sprite</span>:<span class="hljs-title">create</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-string" style="color: rgb(0, 136, 0);">"normal.png"</span>)</span>
<span class="hljs-title">sprite</span>:<span class="hljs-title">setPosition</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(winSize.width/<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>, winSize.height/<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)</span>
<span class="hljs-title">self</span>:<span class="hljs-title">addChild</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(sprite)</span>

-- <span class="hljs-title">CallFunc</span>回调动作
<span class="hljs-title">local</span> <span class="hljs-title">call</span> = <span class="hljs-title">cc</span>.<span class="hljs-title">CallFunc</span>:<span class="hljs-title">create</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(callbackFunc, {x=<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span> , y=<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>} )</span>
<span class="hljs-title">sprite</span>:<span class="hljs-title">runAction</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(call)</span>
</span></code>


5.2、定时器刷新 schedule

在C++中,定时器参见:http://shahdza.blog.51cto.com/2410787/1542014

在Lua中,有两种方式:

(1)self:scheduleUpdateWithPriorityLua(update, priority)

参数一:刷新函数

参数二:刷新优先级

其中 self 为 Node类 的子类。

该方法默认为每帧都刷新一次,无法自定义刷新时间间隔。

(2)scheduler:scheduleScriptFunc(update, inteval, false)

参数一:刷新函数

参数二:每次刷新的时间间隔

参数三:是否只执行一次。false为无限次。

其中 scheduler 为定时器管理:cc.Director:getInstance():getScheduler()

而我更推荐使用第二种方式,因为比较通用。

使用举例:
<code>    local scheduler, myupdate
local timer = 0

local function <span class="hljs-operator"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">update</span>(dt)
cclog(<span class="hljs-string" style="color: rgb(0, 136, 0);">"update: "</span> .. dt) <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 输出log</span>
timer = timer + dt
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> timer >= <span class="hljs-number" style="color: rgb(0, 102, 102);">3</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">then</span>                                  <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 执行3秒取消定时器</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- self:unscheduleUpdate()                  -- 取消定时器</span>
scheduler:unscheduleScriptEntry(myupdate)   <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 取消定时器</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 每帧执行一次update,优先级为0</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- self:scheduleUpdateWithPriorityLua(update, 0);</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 每30/60秒执行一次update,会无限执行</span>
scheduler = cc.Director:getInstance():getScheduler()
myupdate = scheduler:scheduleScriptFunc(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">update</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">30.0</span> / <span class="hljs-number" style="color: rgb(0, 102, 102);">60.0</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102);">false</span>)
</span></code>


5.3、菜单项事件回调 menuItem

在C++中,菜单项回调参见:http://shahdza.blog.51cto.com/2410787/1553051

在Lua中,使用:registerScriptTapHandler(hander) 。

其中 hander 即为需要绑定的回调函数,至于回调函数中有什么参数,参见下面的例子。

使用举例:
<code>    local menu
local normal, hard

-- tag      : 为menuItem设置的标签setTag
-- menuItem : 执行回调的menuItem对象
local <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> <span class="hljs-title">menuCallback</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(tag, menuItem)</span>
<span class="hljs-title">print</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-string" style="color: rgb(0, 136, 0);">"menuItem: "</span> .. tag)</span>
<span class="hljs-title">end</span>

<span class="hljs-title">normal</span> = <span class="hljs-title">cc</span>.<span class="hljs-title">MenuItemImage</span>:<span class="hljs-title">create</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-string" style="color: rgb(0, 136, 0);">"normal.png"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"normal.png"</span>)</span>
<span class="hljs-title">normal</span>:<span class="hljs-title">setPosition</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">120</span>)</span>
<span class="hljs-title">normal</span>:<span class="hljs-title">setTag</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>)</span>

<span class="hljs-title">hard</span> = <span class="hljs-title">cc</span>.<span class="hljs-title">MenuItemImage</span>:<span class="hljs-title">create</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-string" style="color: rgb(0, 136, 0);">"hard.png"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"hard.png"</span>)</span>
<span class="hljs-title">hard</span>:<span class="hljs-title">setPosition</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">20</span>)</span>
<span class="hljs-title">hard</span>:<span class="hljs-title">setTag</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)</span>

-- 创建菜单,最后不需要加<span class="hljs-title">NULL</span>
<span class="hljs-title">menu</span> = <span class="hljs-title">cc</span>.<span class="hljs-title">Menu</span>:<span class="hljs-title">create</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(normal, hard)</span>
<span class="hljs-title">self</span>:<span class="hljs-title">addChild</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(menu)</span>

-- 菜单项回调
<span class="hljs-title">normal</span>:<span class="hljs-title">registerScriptTapHandler</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(menuCallback)</span>   -- 按钮事件
<span class="hljs-title">hard</span>:<span class="hljs-title">registerScriptTapHandler</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(menuCallback)</span>     -- 按钮事件
</span></code>


5.4、按钮控件事件回调 ControlButton

在C++中,按钮控件事件回调参见:http://shahdza.blog.51cto.com/2410787/1543349

在Lua中,使用:registerControlEventHandler(hander, type) 。

其中 hander 为我们需要绑定的回调函数,而 type 则是按钮事件的类型。

至于回调函数中有什么参数,参见下面的例子。

对于 type ,有以下几种类型:
<code>    cc.CONTROL_EVENTTYPE_TOUCH_DOWN         <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 刚刚开始触摸按钮时</span>
cc.CONTROL_EVENTTYPE_DRAG_INSIDE        <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 在内部拖动时(保持触摸状态下)</span>
cc.CONTROL_EVENTTYPE_DRAG_OUTSIDE       <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 在外部拖动时(保持触摸状态下)</span>
cc.CONTROL_EVENTTYPE_DRAG_ENTER         <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 拖动刚进入内部时(保持触摸状态下)</span>
cc.CONTROL_EVENTTYPE_DRAG_EXIT          <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 拖动刚离开内部时(保持触摸状态下)</span>
cc.CONTROL_EVENTTYPE_TOUCH_UP_INSIDE    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 在内部抬起手指(保持触摸状态下)</span>
cc.CONTROL_EVENTTYPE_TOUCH_UP_OUTSIDE   <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 在外部抬起手指(保持触摸状态下)</span>
cc.CONTROL_EVENTTYPE_TOUCH_CANCEL       <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 取消触点时</span>
cc.CONTROL_EVENTTYPE_VALUE_CHANGED      <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 按钮控件中值发生改变时</span>
</code>


使用举例:
<code>    -- node : 执行回调的按钮对象
-- <span class="hljs-built_in" style="color: rgb(102, 0, 102);">type</span> : 按钮事件的类型
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">local</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> btnCallback(node, <span class="hljs-built_in" style="color: rgb(102, 0, 102);">type</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">type</span> == cc.CONTROL_EVENTTYPE_TOUCH_DOWN <span class="hljs-keyword" style="color: rgb(0, 0, 136);">then</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">print</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">"touch down"</span>)
elseif <span class="hljs-built_in" style="color: rgb(102, 0, 102);">type</span> == cc.CONTROL_EVENTTYPE_DRAG_INSIDE <span class="hljs-keyword" style="color: rgb(0, 0, 136);">then</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">print</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">"drag inside"</span>)
elseif <span class="hljs-built_in" style="color: rgb(102, 0, 102);">type</span> == cc.CONTROL_EVENTTYPE_TOUCH_UP_INSIDE <span class="hljs-keyword" style="color: rgb(0, 0, 136);">then</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">print</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">"touch up inside"</span>)
end
end

-- 添加一个按钮 ControlButton
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">local</span> label = cc.Label:createWithSystemFont(<span class="hljs-string" style="color: rgb(0, 136, 0);">"button"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0);">"res/fonts/Marker Felt.ttf"</span>,<span class="hljs-number" style="color: rgb(0, 102, 102);">30</span>)
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">local</span> sprite = cc.Scale9Sprite:create(<span class="hljs-string" style="color: rgb(0, 136, 0);">"normal.png"</span>)
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">local</span> btn = cc.ControlButton:create(label,sprite)
btn:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">set</span>Position(winSize.width/<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>, winSize.height/<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)
self:addChild(btn)

-- 按钮事件回调
btn:registerControlEventHandler(btnCallback,cc.CONTROL_EVENTTYPE_TOUCH_DOWN)
btn:registerControlEventHandler(btnCallback,cc.CONTROL_EVENTTYPE_DRAG_INSIDE)
btn:registerControlEventHandler(btnCallback,cc.CONTROL_EVENTTYPE_TOUCH_UP_INSIDE)
</code>


6、Lua中的事件分发机制

在C++中,事件分发机制参见:http://shahdza.blog.51cto.com/2410787/1560222

事件分发机制包含:

触摸事件 : EventListenerTouchOneByOne、EventListenerTouchAllAtOnce

鼠标响应事件 : EventListenerMouse

键盘响应事件 : EventListenerKeyboard

加速计事件 : EventListenerAcceleration

自定义事件 : EventListenerCustom

物理碰撞事件 : EventListenerPhysicsContact

游戏手柄事件 : EventListenerController

而在cocos2dx v3.2版本中,Lua的使用方式与C++有点类似。

PS:在Lua中对于 鼠标事件、自定义事件 的支持有BUG,建议大家暂时不要去使用。

另外由于博主还未接触过物理碰撞事件、游戏手柄事件,所以也无法讲解着两个事件机制的使用方法,不过可以确定的一点是:以上几个事件处理都是通过监听器来完成的,所以用法应该都是差不多的。

在这里我重点讲一下:触摸事件、键盘事件、加速计事件。

事件处理的原理可以参考C++的使用方法,这里讲一下 Lua 中如何使用。

6.1、使用步骤

还是继续用代码来讲解吧,一般性都分为以下四个步骤。
<code>    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 1.获取事件分发器  : EventDispatcher</span>
local dispatcher = cc.Director:getInstance():getEventDispatcher()

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 2.创建事件监听器  : EventListener (这里以单点触摸为例)</span>
local listener = cc.EventListenerTouchOneByOne:<span class="hljs-operator"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">create</span>()

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 3.注册事件响应函数: registerScriptHandler</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- hander : 响应函数</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- type   : 事件类型</span>
listener:registerScriptHandler(hander, type)

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 4.在事件分发器中,添加监听器。事件响应委托为self</span>
dispatcher:addEventListenerWithSceneGraphPriority(listener, self)
</span></code>


6.2、单点触摸事件EventListenerTouchOneByOne

使用方法如下:

响应函数的两个参数 touch , event 分别表示:

touch : cc.Touch 。触点信息

event : cc.Event 。事件信息

注册响应函数 registerScriptHandler 中的第二个参数,表示事件类型。

cc.Handler.EVENT_TOUCH_BEGAN : 触摸开始

cc.Handler.EVENT_TOUCH_MOVED : 触摸移动

cc.Handler.EVENT_TOUCH_ENDED : 触摸结束

<code>    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 触摸开始</span>
local function onTouchBegan(touch, event)
print("Touch Began")
local pos = touch:getLocation() <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 获取触点的位置</span>
print(pos.x .. " , " .. pos.y)  <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 输出log</span>
return true                     <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 必须返回true 后边move end才会被处理</span>
<span class="hljs-operator"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 触摸移动</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> onTouchMoved(touch, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">event</span>)
print(<span class="hljs-string" style="color: rgb(0, 136, 0);">"Touch Moved"</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 触摸结束</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> onTouchEnded(touch, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">event</span>)
print(<span class="hljs-string" style="color: rgb(0, 136, 0);">"Touch Ended"</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 注册单点触摸</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> dispatcher = cc.Director:getInstance():getEventDispatcher()
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> listener = cc.EventListenerTouchOneByOne:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">create</span>()

listener:registerScriptHandler(onTouchBegan, cc.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Handler</span>.EVENT_TOUCH_BEGAN)
listener:registerScriptHandler(onTouchMoved, cc.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Handler</span>.EVENT_TOUCH_MOVED)
listener:registerScriptHandler(onTouchEnded, cc.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Handler</span>.EVENT_TOUCH_ENDED)

dispatcher:addEventListenerWithSceneGraphPriority(listener, self)
</span></code>


6.3、多点触摸事件EventListenerTouchAllAtOnce

使用方法如下:

响应函数的两个参数 touches , event 分别表示:

touches : cc.Touch的table数组 。多个触点信息

event : cc.Event 。事件信息

注册响应函数 registerScriptHandler 中的第二个参数,表示事件类型。

cc.Handler.EVENT_TOUCHES_BEGAN : 多点触摸开始

cc.Handler.EVENT_TOUCHES_MOVED : 多点触摸移动

cc.Handler.EVENT_TOUCHES_ENDED : 多点触摸结束

<code>    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 触摸开始</span>
local function onTouchesBegan(touches, event)
print("Touches Began")
local pos1 = touches[1]:getLocation()   <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 获取触点1的位置</span>
local pos2 = touches[2]:getLocation()   <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 获取触点2的位置</span>
local delta = {
x = pos2.x - pos1.x ,
y = pos2.y - pos1.y
}
print(delta.x .. " , " .. delta.y)      <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 输出log</span>
<span class="hljs-operator"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 触摸移动</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> onTouchesMoved(touches, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">event</span>)
print(<span class="hljs-string" style="color: rgb(0, 136, 0);">"Touches Moved"</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 触摸结束</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> onTouchesEnded(touches, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">event</span>)
print(<span class="hljs-string" style="color: rgb(0, 136, 0);">"Touches Ended"</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 注册多点触摸</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> dispatcher = cc.Director:getInstance():getEventDispatcher()
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> listener = cc.EventListenerTouchAllAtOnce:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">create</span>()

listener:registerScriptHandler(onTouchesBegan, cc.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Handler</span>.EVENT_TOUCHES_BEGAN)
listener:registerScriptHandler(onTouchesMoved, cc.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Handler</span>.EVENT_TOUCHES_MOVED)
listener:registerScriptHandler(onTouchesEnded, cc.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Handler</span>.EVENT_TOUCHES_ENDED)

dispatcher:addEventListenerWithSceneGraphPriority(listener, self)
</span></code>


6.4、键盘事件EventListenerKeyboard

EventListenerKeyboard,主要用于监听键盘某个键的按下、松开的事件。

使用方法如下:

响应函数的两个参数 keyCode , event 分别表示:

keyCode : number 。键盘按键枚举值

event : cc.Event 。事件信息

注册响应函数 registerScriptHandler 中的第二个参数,表示事件类型。

cc.Handler.EVENT_KEYBOARD_PRESSED : 按下键盘的某个键

cc.Handler.EVENT_KEYBOARD_RELEASED : 松开键盘的某个键

键盘按键枚举值如下:(仅举例说明)

cc.KeyCode.KEY_A : A键

cc.KeyCode.KEY_1 : 1键

cc.KeyCode.KEY_F1 : F1键

cc.KeyCode.KEY_SPACE : 空格键

cc.KeyCode.KEY_ALT : ALT键

cc.KeyCode.KEY_SHIFT : SHIFT键

<code>    local function onKeyPressed(keyCode, event)
if keyCode == cc.KeyCode.KEY_A then
print("Pressed A !")  <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 按下A键</span>
<span class="hljs-operator"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> onKeyReleased(keyCode, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">event</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> keyCode == cc.KeyCode.KEY_J <span class="hljs-keyword" style="color: rgb(0, 0, 136);">then</span>
print(<span class="hljs-string" style="color: rgb(0, 136, 0);">"Released J !"</span>) <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 松开J键</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">end</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 注册键盘事件</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> dispatcher = cc.Director:getInstance():getEventDispatcher()
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">local</span> listener = cc.EventListenerKeyboard:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">create</span>()

listener:registerScriptHandler(onKeyPressed, cc.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Handler</span>.EVENT_KEYBOARD_PRESSED)
listener:registerScriptHandler(onKeyReleased, cc.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Handler</span>.EVENT_KEYBOARD_RELEASED)

dispatcher:addEventListenerWithSceneGraphPriority(listener, self)
</span></code>


6.5、加速计事件EventListenerAcceleration

EventListenerAcceleration,主要用于监听移动设备的所受重力方向感应事件。

重力感应来自移动设备的加速计,通常支持 (X, Y, Z) 三个方向的加速度感应,所以又称为三向加速计。在实际应用中,可以根据3个方向的力度大小来计算手机倾斜的角度或方向。

EventListenerAcceleration用法和上述几个有点不同。

使用方法如下:

响应函数有五个参数 event , x , y , z , timestamp 分别表示:

event : cc.Event 。事件信息

(x, y, z) : number 。设备在三个方向上的重力加速度感应

timestamp : number 。响应事件的时间戳

创建加速计监听器时,直接传入响应函数作为参数,而不用registerScriptHandler 。

使用加速计事件时,还需要开启设备的加速计感应:

self:setAccelerometerEnabled(true)

在电脑上看不到效果,需要在设备上才能看到效果。

<code>    -- 开启设备的加速计感应
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>:setAccelerometerEnabled(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">true</span>)

-- 响应函数
local <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> <span class="hljs-title">onAccelerationEvent</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(event, x, y, z, timestamp)</span>
<span class="hljs-title">print</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-string" style="color: rgb(0, 136, 0);">"x: "</span> .. x)</span>
<span class="hljs-title">print</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-string" style="color: rgb(0, 136, 0);">"y: "</span> .. y)</span>
<span class="hljs-title">print</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-string" style="color: rgb(0, 136, 0);">"z: "</span> .. z)</span>
<span class="hljs-title">print</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(<span class="hljs-string" style="color: rgb(0, 136, 0);">"timestamp: "</span> .. timestamp)</span>
<span class="hljs-title">end</span>

-- 注册加速计监听器
<span class="hljs-title">local</span> <span class="hljs-title">dispatcher</span> = <span class="hljs-title">cc</span>.<span class="hljs-title">Director</span>:<span class="hljs-title">getInstance</span><span class="hljs-params" style="color: rgb(102, 0, 102);">()</span>:<span class="hljs-title">getEventDispatcher</span><span class="hljs-params" style="color: rgb(102, 0, 102);">()</span>

-- 直接传入 响应函数 作为参数
<span class="hljs-title">local</span> <span class="hljs-title">listener</span> = <span class="hljs-title">cc</span>.<span class="hljs-title">EventListenerAcceleration</span>:<span class="hljs-title">create</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(onAccelerationEvent)</span>

<span class="hljs-title">dispatcher</span>:<span class="hljs-title">addEventListenerWithSceneGraphPriority</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(listener, self)</span>
</span></code>


在手机上的运行效果:



7、Lua中的数学类

在C++中,数学类参见:http://shahdza.blog.51cto.com/2410787/1550972

数学类主要有Vec2(坐标向量)、Size(尺寸)、Rect(矩形)。

7.1、创建

在Lua中创建的 Vec2、Size、Rect 都是一个table类型。

其中只有相应的成员变量,没有相关的函数运算。
<code>    cc.p(x, y)                    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 构造 Vec2</span>
cc.size(width, height)        <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 构造 Size</span>
cc.rect(x, y, width, height)  <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 构造 Rect</span>
</code>


7.2、坐标向量运算

Lua中常用的向量运算如下:

(1)返回值为bool
<code>    -- 直线AB与直线CD是否相交
cc.pIsLineIntersect(pA, pB, pC, pD,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">float</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">float</span>)

-- 线段AB与线段CD是否相交
cc.pIsSegmentIntersect(pA, pB, pC, pD)
</code>


(2)返回值为float
<code>    cc.pDot(p1, p2)         <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 点积</span>
cc.pCross(p1, p2)       <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 叉积</span>
cc.pProject(p1, p2)     <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 投影: 前point在后point上的投影</span>

cc.pGetLength(p)        <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 向量长度 </span>
cc.pLengthSQ(p)         <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 向量长度平方</span>

cc.pGetDistance(p1, p2) <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 坐标距离</span>
cc.pDistanceSQ(p1, p2)  <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 坐标距离平方</span>

cc.pGetAngle(p1, p2)    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 向量夹角:弧度</span>
</code>


(3)返回值为point_table
<code>    cc.p(x, y)                       <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 构造坐标point</span>
cc.pAdd(p1, p2)                  <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 相加</span>
cc.pSub(p1, p2)                  <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 相减</span>
cc.pMidpoint(p1, p2)             <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 两向量的中点</span>
cc.pNormalize(p1)                <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 标准化向量</span>

cc.pGetClampPoint(minp, maxp, p) <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 将p值限制在[minp,maxp]区间内</span>

cc.pForAngle(float)              <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 返回坐标 x=cos(a) , y=sin(a)</span>

cc.pPerp(p)                      <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 逆时针旋转90度(-y, x)</span>
cc.RPerp(p)                      <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 顺时针旋转90度(y, -x)</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 绕p1向量旋转</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 返回向量: 角度 this.getAngle() +other.getAngle()</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">--           长度 this.getLength()*other.getLength()</span>
cc.pRotate(p1, p2)

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 绕p1向量旋转前的向量值</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 返回向量: 角度 this.getAngle() -other.getAngle(); </span>
<span class="hljs-comment" style="color: rgb(136, 0, 0);">--           长度 this.getLength()*other.getLength();</span>
cc.pUnrotate(p1, p2)

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 直线AB与直线CD的交点</span>
cc.pGetIntersectPoint(pA, pB, pC, pD)
</code>


7.3、矩形运算

Lua中常用的矩形运算如下:
<code>    cc.rectGetMinX(rect)    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- rect.x</span>
cc.rectGetMidX(rect)    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- (rect.x + rect.width) / 2</span>
cc.rectGetMaxX(rect)    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- rect.x + rect.width</span>

cc.rectGetMinY(rect)    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- rect.y</span>
cc.rectGetMidY(rect)    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- (rect.y + rect.height) / 2</span>
cc.rectGetMaxY(rect)    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- rect.y + rect.height</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 判断是否与rect相同. 原点相同,尺寸相同.</span>
cc.rectEqualToRect(rect1, rect2)

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 判断point是否包含在矩形内</span>
cc.rectContainsPoint(rect, point)

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 判断矩形是否相交. 常常用作碰撞检测.</span>
cc.rectIntersectsRect(rect1, rect2)

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 两矩形合并</span>
cc.rectUnion(rect1, rect2)
</code>


8、Lua中的颜色类

在C++的颜色类主要有三个:Color3B、Color4B、Color4F。并且有很多颜色的宏定义。

在Lua中,无法使用颜色的宏定义,而需要自己设置。

颜色类的使用方法如下:
<code>    cc.c3b(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">byte</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">byte</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">byte</span>)              -- 构造 Color3B
cc.c4b(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">byte</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">byte</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">byte</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">byte</span>)         -- 构造 Color4B
cc.c4f(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">float</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">float</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">float</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">float</span>)     -- 构造 Color4F
</code>


9、还有什么?

在最后,我还想讲讲一些使用Lua开发cocos2dx的注意事项。

9.1、关于getPosition()

先来看看几个 getXXX() 函数的返回值吧。
<code>    getPosition()      <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 返回两个值:x y</span>
getAnchorPoint()   <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- point_table</span>
getContentSize()   <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- size_table</span>
getBoundingBox()   <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- rect_table</span>
</code>
<code>可以发现:getPosition() 返回的不是point_table,而是两个值。
PS:真是一个大坑!
所以接收 getPosition() 时需要处理一下。
</code>
<code>    <span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 方法一 : 用两个变量接收</span>
local x, y = sprite:getPosition()

<span class="hljs-comment" style="color: rgb(136, 0, 0);">-- 方法二 : 转换为point_table</span>
local p = cc.p(sprite:getPosition())
</code>


9.2、关于 . 和 :

在Lua中,一定要区分这两个:点. 和 冒号: 的区别,很容易搞混掉。

定义的时候,冒号: 默认接收self参数

调用的时候,冒号: 默认传递调用者自己为参数

而点号. 要显示传递或接收self参数

例如:点号定义,需要显示传递或接收。

<code>    tab = { x = <span class="hljs-number" style="color: rgb(0, 102, 102);">1</span> }

<span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> <span class="hljs-title">tab</span>.<span class="hljs-title">fun</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(self)</span>
<span class="hljs-title">print</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(self.x)</span>
<span class="hljs-title">end</span>

<span class="hljs-title">a</span>.<span class="hljs-title">fun</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(a)</span>    -- 需要将<span class="hljs-title">a</span>本身做为参数传给<span class="hljs-title">fun</span>函数
</span></code>


这样使用函数就要传递自身self,用起来很麻烦。

所以Lua给出了一个方便的方法:将函数定义改成冒号: ,这样就可以省略self参数了。
<code>    a = { x = <span class="hljs-number" style="color: rgb(0, 102, 102);">1</span> }

<span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> <span class="hljs-title">a</span>:<span class="hljs-title">fun</span><span class="hljs-params" style="color: rgb(102, 0, 102);">()</span>
<span class="hljs-title">print</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(self.x)</span>
<span class="hljs-title">end</span>

<span class="hljs-title">a</span>:<span class="hljs-title">fun</span><span class="hljs-params" style="color: rgb(102, 0, 102);">()</span></span></code>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: