Cocos2d-Lua 3.12 生成绑定 导出C++类给LUA调用( tolua genbindi
工具:Python、pyyaml、pyCheetah都用32位版本(可避免很多错误) (ps:tolua目录下README.mdown里有下载链接) frameworkscocos2d-xtoolstoluaREADME.mdown 例子: MyClass.h
#include"MyClass.h" 生成所需的配置文件 MyClass.ini 重点提示 可从其他.ini文件复制修改 [MyClass] # the prefix to be added to the generated functions. You might or might not use this in your own # templates修改 prefix =MyClass # all classes will be embedded in that namespace修改 #修改 生成frameworkscocos2d-xtoolstoluaMyGenbindings.py tolua_root = '%s/tools/tolua' % project_root cmd_args = {#'cocos2dx.ini' : ('cocos2d-x','lua_cocos2dx_auto'), 运行pythonmygenbindings.py 即生成在输出目录frameworkscocos2d-xtoolstoluaauto 遇到问题参考: 一、How to Use bindings-generator 二、python version 2.7 required,which was not found in the registry 方法:新建一个register.py文件,把一下代码贴进去,保存 # # script to register Python 2.0 or later for use with win32all # and other extensions that require Python registry settings # written by Joakim Loew for Secret Labs AB / PythonWare # source: # http://www.pythonware.com/products/works/articles/regpy20.htm # modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html importsys from_winreg* # tweak as necessary version = sys.version[:3] installpath = sys.prefix
regpath ="SOFTWAREPythonPythoncore%s"% (version) installkey ="InstallPath" pythonkey ="PythonPath" pythonpath ="%s;%sLib;%sDLLs"% ( installpath,installpath,installpath ) defRegisterPy(): try: reg = OpenKey(HKEY_CURRENT_USER,regpath) exceptEnvironmentError as e: reg = CreateKey(HKEY_CURRENT_USER,regpath) SetValue(reg,installkey,REG_SZ,installpath) CloseKey(reg) : print"*** Unable to register!" return "--- Python",version,"is now registered!" if(QueryValue(reg,installkey) == installpathand QueryValue(reg,pythonkey) == pythonpath): "=== Python""is already registered!" return "*** You probably have another Python installation!" __name__=="__main__"RegisterPy() win7是 64的原因,在安装python(32位)时,如果选择只为当前用户,以上问题是不会出现的,如果选择所有用户,那就用上面的方法解决吧。三、TranslationUnitLoadError: Error parsing translation unit. 的提示错误,基本都是.ini文件没有配置正确,仔细检查一下 .ini文件里的 “headers =”指向的路径是否正确 四、ImportError: No module named Cheetah.Template 从错误提示就知道缺少cheetah库,所以先从http://pythonhosted.org//Cheetah/这个地址下载库,打开终端,cd 到下载文件夹的目录, 输入命令:sudopythonsetup.py install即可 如果是windows提示这个错误,那么也是下载Cheetan这个库,然后安装 五、
使用PYTHONPATH环境变量,在这个环境变量中输入相关的路径,不同的路径之间用逗号(英文的!)分开,如果PYTHONPATH 变量还不存在,可以创建它! 这里的路径会自动加入到sys.path中,而且可以在不同的python版本中共享,应该是一样较为方便的方法。 unzip it to "C:Python27Libsite-packages" 若运行出现错误,可以 设置环境变量:PYTHONPATH D:AndroidWin764Python27libssite-packages 如何使用生成的文件: 一、把生成的文件添加至项目中,编辑AppDelegate.cpp文件 修改applicationDidFinishLaunching方法,register_all_*加上是生成的的类名。必须放在engine->executeScriptFile前面。 // register lua moduleauto engine = LuaEngine::getInstance(); ScriptEngineManager::getInstance()->setScriptEngine(engine); lua_State* L = engine->getLuaStack()->getLuaState(); lua_module_register(L); //frameworkscocos2d-xtoolstoluaautolua_MyClass_auto.cpp //register_all_MyClass(lua_State* tolua_S) register_all_MyClass(engine->getLuaStack()->getLuaState()); if (engine->executeScriptFile("src/main.lua")) 然后,可以编辑LUA脚本了。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |