下面介绍一下 自己新添加一个C++类 如何绑定到js!
我们建立自己要绑定的类
测试用的代码很好懂
// CustomClass.h
#ifndef __CUSTOM__CLASS
#define __CUSTOM__CLASS
#include “cocos2d.h”
namespace cocos2d {
class CustomClass : public cocos2d::Ref
{
public:
CustomClass();
~CustomClass();
bool init();
std::string helloMsg();
CREATE_FUNC(CustomClass);
};
} //namespace cocos2d
#endif // __CUSTOM__CLASS
// CustomClass.cpp
#include “CustomClass.h”
USING_NS_CC;
CustomClass::CustomClass(){
}
CustomClass::~CustomClass(){
}
bool CustomClass::init(){
return true;
}
std::string CustomClass::helloMsg() {
return “Hello from CustomClass::sayHello”;
}
用xcdoe 打开 frameworks/runtime-src/proj.ios_mac/CocosJSGame.xcodeproj 添加 自己的文件 (windows VS党 后面的仅供参考…)
注意俩个lib 都勾选上!
添加后

选中 cocos2d_js_bindings.xcodeproj 添加刚才的引用路径
下面的也要添加
然后再 项目->tools->tojs 目录下 新建 cocos2dx_custom.ini 文件
复制如下内容
the prefix to be added to the generated functions. You might or might not use this in your own
templates
prefix = cocos2dx_custom
create a target namespace (in javascript, this would create some code like the equiv. to ns = ns || {})
all classes will be embedded in that namespace
target_namespace = cc
android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/include
android_flags = -D_SIZE_T_DEFINED_
clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include
clang_flags = -nostdinc -x c++ -std=c++11
cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/my -I%(cocosdir)s/cocos/base -I%(cocosdir)s/cocos/platform/android
cocos_flags = -DANDROID -DCOCOS2D_JAVASCRIPT
cxxgenerator_headers =
extra arguments for clang
extra_arguments = (android_headers)s (clang_headers)s (cxxgenerator_headers)s (cocos_headers)s (android_flags)s (clang_flags)s (cocos_flags)s (extra_flags)s
what headers to parse
headers = %(cocosdir)s/cocos/my/CustomClass.h
what classes to produce code for. You can use regular expressions here. When testing the regular
expression, it will be enclosed in “^", like this: "^Menu*”.
classes = CustomClass.*
what should we skip? in the format ClassName::
ClassName is a regular expression, but will be used like this: “^ClassName$” functions are also
regular expressions, they will not be surrounded by “^$”. If you want to skip a whole class, just
add a single “" as functions. See bellow for several examples. A special class name is "”, which
will apply to all class names. This is a convenience wildcard to be able to skip similar named
functions from all classes.
skip =
rename_functions =
rename_classes =
for all class names, should we remove something when registering in the target VM?
remove_prefix =
classes for which there will be no “parent” lookup
classes_have_no_parents =
base classes which will be skipped when their sub-classes found them.
base_classes_to_skip = Ref Clonable
classes that create no constructor
Set is special and we will use a hand-written constructor
abstract_classes =
Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are ‘yes’ or ‘no’.
script_control_cpp = no
注意 其中
headers = %(cocosdir)s/cocos/my/CustomClass.h
…
classes = CustomClass.*
名字要改成 自己要添加的类名
其中 还有一些配置 例如跳过某些类方法 skip = 等等 大家可以自己研究官方的 ini文件
打开 tools/tojs/genbindings.py
在 custom_cmd_args 添加 ‘cocos2dx_custom.ini’ : (‘cocos2dx_custom’, ‘jsb_cocos2dx_custom’), \
修改完成如下
custom_cmd_args = {
‘cocos2dx_custom.ini’ : (‘cocos2dx_custom’, ‘jsb_cocos2dx_custom’),
}
接下来运行 python genbindings.py 等待成功!
完事以后会生成你自己 hpp 和 cpp 以及 js api
用Xcode打开 项目 如果没有 jsb_cocos2dx_custom.hpp 和 jsb_cocos2dx_custom.cpp 就添加以下 记得 mac ios lib 都要勾选上

此时恭喜你已经大功告成!
打开AppDelegate.cpp 添加 addRegisterCallback(register_all_cocos2dx_custom) 进行注册
…
#include “jsb_cocos2dx_custom.hpp”
…
sc->addRegisterCallback(register_all_cocos2dx_custom);
#if (COCOS2D_DEBUG>0)
if (startRuntime())
return true;
#endif
ScriptingCore::getInstance()->start();
在js中调用
var customClass = cc.CustomClass.create();
var msg = customClass.helloMsg()
cc.log("customClass’s msg is : " + msg)
输出 customClass’s msg is : Hello from CustomClass::sayHello
就此自动绑定C++类 完毕! 有什么问题欢迎大家讨论!