工具:Python、pyyaml、pyCheetah都用32位版本 (可避免很多错误)
(ps:tolua目录下README.mdown里有下载链接)
frameworks\cocos2d-x\tools\tolua\README.mdown
例子:
MyClass.h
#include "cocos2d.h"
using namespace cocos2d;
class MyClass : public Ref
{public:
MyClass() {};
~MyClass() {};
bool init() { return true; };
CREATE_FUNC(MyClass);
int foo(int i);
};
#include "MyClass.h"
int MyClass::foo(int i)
{
return i + 100;
}
[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修改
target_namespace = my
#修改
#headers = %(cocosdir)s/../runtime-src/Classes/MyClass.h
headers = MyClass.h
#修改
classes = MyClass
生成frameworks\cocos2d-x\tools\tolua\MyGenbindings.py
tolua_root = '%s/tools/tolua' % project_root
output_dir = '%s/tools/tolua/auto' % project_root 输出目录,可自定义,根据需要
cmd_args = {#'cocos2dx.ini' : ('cocos2d-x', 'lua_cocos2dx_auto'), \
#'cocos2dx_extension.ini' : ('cocos2dx_extension', 'lua_cocos2dx_extension_auto'), \
#'cocos2dx_ui.ini' : ('cocos2dx_ui', 'lua_cocos2dx_ui_auto'), \
#'cocos2dx_studio.ini' : ('cocos2dx_studio', 'lua_cocos2dx_studio_auto'), \
#'cocos2dx_spine.ini' : ('cocos2dx_spine', 'lua_cocos2dx_spine_auto'), \
#'cocos2dx_physics.ini' : ('cocos2dx_physics', 'lua_cocos2dx_physics_auto'), \
#'cocos2dx_experimental_video.ini' : ('cocos2dx_experimental_video', 'lua_cocos2dx_experimental_video_auto'), \
#'cocos2dx_experimental.ini' : ('cocos2dx_experimental', 'lua_cocos2dx_experimental_auto'), \
#'cocos2dx_controller.ini' : ('cocos2dx_controller', 'lua_cocos2dx_controller_auto'), \
#'cocos2dx_cocosbuilder.ini': ('cocos2dx_cocosbuilder', 'lua_cocos2dx_cocosbuilder_auto'), \
#'cocos2dx_cocosdenshion.ini': ('cocos2dx_cocosdenshion', 'lua_cocos2dx_cocosdenshion_auto'), \
#'cocos2dx_3d.ini': ('cocos2dx_3d', 'lua_cocos2dx_3d_auto'), \
#'cocos2dx_audioengine.ini': ('cocos2dx_audioengine', 'lua_cocos2dx_audioengine_auto'), \
#'cocos2dx_csloader.ini' : ('cocos2dx_csloader', 'lua_cocos2dx_csloader_auto'), \
#'cocos2dx_experimental_webview.ini' : ('cocos2dx_experimental_webview', 'lua_cocos2dx_experimental_webview_auto'), \
#'cocos2dx_physics3d.ini' : ('cocos2dx_physics3d', 'lua_cocos2dx_physics3d_auto'), \
#'cocos2dx_navmesh.ini' : ('cocos2dx_navmesh', 'lua_cocos2dx_navmesh_auto'), \
'MyClass.ini' : ('MyClass', 'lua_MyClass_auto'), 根据需要添加的
}
运行python mygenbindings.py 即生成在输出目录frameworks\cocos2d-x\tools\tolua\auto
遇到问题参考:
一、How to Use bindings-generator
==================
On Windows:
------------
* Make sure that you have installed `android-ndk-r10c` or later.
* Download python2.7.3 (32bit) from (http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi).
* Add the installed path of python (e.g. C:\Python27) to windows environment variable named 'PATH'.
* Download pyyaml from http://pyyaml.org/download/pyyaml/PyYAML-3.11.win32-py2.7.exe and install it.
* Download pyCheetah from https://raw.github.com/dumganhar/my_old_cocos2d-x_backup/download/downloads/Cheetah.zip, unzip it to "C:\Python27\Lib\site-packages"
* Set environment variables `NDK_ROOT` and `PYTHON_BIN`
* Go to "cocos2d-x/tools/tolua" folder, and run "genbindings.py". The generated codes will be under "cocos\scripting\auto-generated\lua-bindings".
二、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/[email protected]/msg10512.html
import sys
from _winregimport *
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
regpath ="SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey ="InstallPath"
pythonkey ="PythonPath"
pythonpath ="%s;%s\\Lib\\;%s\\DLLs\\" % (
installpath, installpath, installpath
)
def RegisterPy():
try:
reg = OpenKey(HKEY_CURRENT_USER, regpath)
except EnvironmentError as e:
try:
reg = CreateKey(HKEY_CURRENT_USER, regpath)
SetValue(reg, installkey, REG_SZ, installpath)
SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
except:
print "*** Unable to register!"
return
print "--- Python", version, "is now registered!"
return
if (QueryValue(reg, installkey) == installpathand
QueryValue(reg, pythonkey) == pythonpath):
CloseKey(reg)
print "=== Python", version, "is already registered!"
return
CloseKey(reg)
print "*** Unable to register!"
print "*** You probably have another Python installation!"
if __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 到下载文件夹的目录,
输入命令:sudo python setup.py install即可
如果是windows提示这个错误,那么也是下载Cheetan这个库,然后安装
五、
使用PYTHONPATH环境变量,在这个环境变量中输入相关的路径,不同的路径之间用逗号(英文的!)分开,如果PYTHONPATH 变量还不存在,可以创建它!
这里的路径会自动加入到sys.path中,而且可以在不同的python版本中共享,应该是一样较为方便的方法。
unzip it to "C:\Python27\Lib\site-packages" 若运行出现错误,可以
设置环境变量:PYTHONPATH
D:\AndroidWin764\Python27\libs\site-packages
如何使用生成的文件:
一、把生成的文件添加至项目中,编辑AppDelegate.cpp文件
修改applicationDidFinishLaunching方法,register_all_*加上是生成的的类名。必须放在engine->executeScriptFile前面。
// register lua module ScriptEngineManager::getInstance()->setScriptEngine(engine);
lua_State* L = engine->getLuaStack()->getLuaState();
lua_module_register(L);
//frameworks\cocos2d-x\tools\tolua\auto\lua_MyClass_auto.cpp
//register_all_MyClass(lua_State* tolua_S)
register_all_MyClass(engine->getLuaStack()->getLuaState());
register_all_packages();
if (engine->executeScriptFile("src/main.lua"))
{
return false;
}
然后,可以编辑LUA脚本了。