使用Eclipse来开发lua。使用eclipse开发lua,可以运用其强大的扩展能力,大大地加快开发。文章将介绍怎么定制一个工具来生成lua,并进行调用。
开发工具下载
使eclipse开发lua有两种方法: 1)安装ldt; 2)下载整合好的eclipse;
ldt的安装就不介绍了,大家可以到网上去搜索关于eclipse插件安装的教程;官方也有相应的说明:http://www.eclipse.org/koneki/ldt/#documentation
如果要安装c++相关的功能,还需要安装cdt,cdt的官方如下:http://www.eclipse.org/cdt/downloads.php
工程的创建
创建一个工程,叫luaTester; 然后编辑main.lua,运行,结果如下:
调试界面:
配置toLua++工具
在cocos2dx中开发lua,需要用到toLua++来生成文件。网上已经有相关教程,不过都不是很全面,操作也不是很方便。我在这里利用eclipse强大的工具配置功能来定制toLua++,使得开发更有效率。
几点说明(假设cocos2dx的根目录为/Users/shhqw132/Documents/cocos2d-2.1rc0-x-2.1.3):
1) 在tools/tolua++目录下有tolua++工具,默认是打包的(windows打包成rar文件;mac下打包成zip文件)。根据情况减压一个到tools/tolua++下;上图中,工具的Location定位到刚刚减压缩出来的可执行文件。
2)Eclipse中内置了一些变量的地址。这里用到了${resource_loc} 这个变量,即当前选择的资源在系统中的绝对路径。
3)toLua++命令格式如下: ./toLua++ -L basic.lua -o xxx.cpp xxx.pkg ; -L:指定相应lua文件的位置(用于修改生成的cpp文件,cocos2dx中用来对tolua++生产的文件做fix); -o 指定的是生成的cpp文件的名称; xxx.pkg是规则文件。
在上图的Arguments中,添加相应的tolua++的参数。
更多关于-L的说明。 由于toLua++不支持回调函数,而cocos2dx中用到了如registerScriptHandler等回调函数,所以cocos2dx对用toLua++生成的代码进行了一些fix;这些fix的规则在tools/tolua++/basic.lua中定义。所以为了支持回调函数,请使用这个文件。
OK,工具配置完毕。
整合开发
MyTest.h
- #ifndef LuaTest01_MyTest_h
- #define LuaTest01_MyTest_h
- #include "cocos2d.h"
- USING_NS_CC;
- class MyTest: public cclayer {
- private:
- int invokeHandler;
- public:
- Mytest() : invokeHandler(0) {}
- CREATE_FUNC(MyTest);
- void setHandler(int _handler) {
- this->invokeHandler = _handler;
- }
- void callFunc() {
- ccluaStack* pStack = ccluaEngine::defaultEngine()->getLuaStack();
- pStack->pushInt(123);
- pStack->executeFunctionByHandler(this->invokeHandler, 1);
- void sayHello() {
- cclog("Hello, world!");
- static void classInfo() {
- cclog("This class is MyTest");
- void otherFunc() {
- //This function doesn't expose to the lua
- };
- #endif
把MyTest.cpp中要暴露的方法,整合到MyTest.pkg中,如下:
copy
- public cclayer {
- static MyTest* create();
- void setHandler(LUA_FUNCTION _handler) ;
- void callFunc() ;
- void sayHello() ;
- void classInfo() ;
- };