【COCOS2DX-LUA 脚本开发之六】Lua强转函数解决报错及介绍两款框架
本站文章均为 李华明Himi 原创,转载务必在明显处注明:(作者新浪微博: @李华明Himi )转载自【黑米GameDev街区】 原文链接: http://www.himigame.com/iphone-cocos2dx/1289.html
☞ 点击订阅 ☜ 本博客最新动态!及时将最新博文通知您!
Him 的Cocos2dx-Lua群中有童鞋出现一个问题,问题是当他在Lua项目中利用Lua创建一个局部变量CCSprite或者CCLayer等CCNode,然后在其他的函数中通过其索引取出之前创建过的CCSprite或CCLayer等,取出后进行设置设置透明、坐标、缩放、runAction等大部分几率都出现无法正常缩放、透明等这些操作。群友尝试了很多方式,都没有解决掉。
其实对于此种问题很多刚接触Lua开发cocos2dx的开发者都很容易遇到,其实就是个强转的问题,例如:
在 cpp中利用getChildByTag 函数获取目标tag的ccnode:
<tr style="border: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-p_w_picpath: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-size: 1em !important; min-height: inherit !important; ">
<td class="gutter" style="border: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-p_w_picpath: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-size: 1em !important; min-height: inherit !important; color: rgb(175, 175, 175) !important; ">
1
2
</td>
<td class="code" style="border: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-p_w_picpath: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 627px; box-sizing: content-box !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; min-height: inherit !important; ">
<code class="cpp plain" style="border: 0px !important; margin: 0px !important; padding: 0px !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; line-height: 1.1em !important; background-p_w_picpath: none !important; overflow: visible !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: inherit !important; background-color: rgb(255, 255, 255); ">CCSprite* sp =(CCSprite*)node->getChildByTag(x);</code>
<code class="cpp plain" style="border: 0px !important; margin: 0px !important; padding: 0px !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; line-height: 1.1em !important; background-p_w_picpath: none !important; overflow: visible !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: inherit !important; background-color: rgb(255, 255, 255); ">CCLayer* layer =(CCLayer*)node->getChildByTag(x)</code>
</td>
</tr>
</tbody>
在Lua中代码如下:
<tr style="border: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-p_w_picpath: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-size: 1em !important; min-height: inherit !important; ">
<td class="gutter" style="border: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-p_w_picpath: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-size: 1em !important; min-height: inherit !important; color: rgb(175, 175, 175) !important; ">
1
2
</td>
<td class="code" style="border: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-p_w_picpath: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 627px; box-sizing: content-box !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; min-height: inherit !important; ">
<span style="background-color: rgb(255, 255, 255); "><code class="py plain" style="border: 0px !important; margin: 0px !important; padding: 0px !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; line-height: 1.1em !important; background-p_w_picpath: none !important; overflow: visible !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: inherit !important; ">local sp </code><code class="py keyword" style="border: 0px !important; margin: 0px !important; padding: 0px !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; line-height: 1.1em !important; color: rgb(0, 102, 153) !important; background-p_w_picpath: none !important; overflow: visible !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-weight: bold !important; min-height: inherit !important; ">=</code> <code class="py plain" style="border: 0px !important; margin: 0px !important; padding: 0px !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; line-height: 1.1em !important; background-p_w_picpath: none !important; overflow: visible !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: inherit !important; ">node:getChildByTag(x)</code></span>
<span style="background-color: rgb(255, 255, 255); "><code class="py plain" style="border: 0px !important; margin: 0px !important; padding: 0px !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; line-height: 1.1em !important; background-p_w_picpath: none !important; overflow: visible !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: inherit !important; ">local layer </code><code class="py keyword" style="border: 0px !important; margin: 0px !important; padding: 0px !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; line-height: 1.1em !important; color: rgb(0, 102, 153) !important; background-p_w_picpath: none !important; overflow: visible !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-weight: bold !important; min-height: inherit !important; ">=</code><code class="py plain" style="border: 0px !important; margin: 0px !important; padding: 0px !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; line-height: 1.1em !important; background-p_w_picpath: none !important; overflow: visible !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: inherit !important; ">node:getChildByTag(x)</code></span>
</td>
</tr>
</tbody>
如上的代码一对比很多童鞋就看出来了,Lua形式的并没有强转类型!是的,就是因为没有强转造成。
还有一些童鞋该疑惑了“为什么cocos2dx引擎示例中就没有使用强转呢?”
原因是由于示例基本都是采用成员变量进行的,局部变量还是有强转的操作的。
那么Lua中强转一个类型函数如下:
tolua.cast(object,”CCSprite”)
第一个参数是需要强转的对象,第二个参数是强转的目标类型
例如我们将x 强转 CCSprite,则: tolua.cast(x,”CCSprite”)
quick-cocos2d-x 与 OpenQuick
OpenQuick:
OpenQuick 首页及下载地址:https://github.com/marmalade/OpenQuick
quick-cocos2d-x :
quick-cocos2d-x 首页及下载地址 : https://github.com/dualface/quick-cocos2d-x
简单的说: OpenQuick 与 quick-cocos2d-x 都是针对 2D游戏 基于 Lua 的免费开源框架,且都支持c2dx。
OpenQuick :主要是将所有的C2dx的类的基础上进行了再次封装成Q开头文件,开发者需在此基础上进行使用开发。
quick-cocos2d-x: 及时与cocos2dx版本匹配更新,另外它框架比较清晰,便于理解和使用。它所做的不是再次封装,然后尽可能契合cocos2dx,在此基础上做Lua的扩展。
Himi认为quick-cocos2d-x更适合一些,而且也是当前开发者使用居多的一款,当然这大家可以更深入的去研究。
页:
[1]