与之前的cocos2dx js自定义js-binding不同,这次用的是Cocos2dx里的自动绑定技术,更加的简单、高效、规整以及方便得多。
而且之前的手动写文件不能适应更新后的CocosCreator版本的情况。
环境配置:
JDK、NDK、SDK、ANT:这些环境变量,当打包成原生平台的时候,已经配置过。
PYTHON:Mac自带有python,一般是python2.7。
等会还会有其他环境配置,主要是在执行genbindings.py的时候,需要配置的环境。等会再说。
当前的CocosCreator环境是1.8.1。版本的不同,会造成一些小的差异,比如1.8.1的jsb_module_register.cpp文件是放在runtime-src/Clesses下的,而之前我用的1.5.1版本是放在cocos/scripting/js-bindings/manual下的。
配置过程:
先自定义自己的头文件Test.h
//
// Test.h
// libcocos2d Mac
//
// Created by zhufu on 2018/3/15.
//
#ifndef Test_h
#define Test_h
#include <stdio.h>
#endif /* Test_h */
class Test : public Node
{
public:
void test();
}
再定义Test.cpp文件
//
// Test.cpp
// libcocos2d Mac
//
// Created by zhufu on 2018/3/15.
//
#include "Test.h"
void Test::test()
{
printf("js binding success!!!!\n");
}
把这两个文件放在以下路径
之后,进入cocos2d-x/tools/tojs文件夹中,
这里我们也要配置一个.ini文件,配置方式就学着coco2dx_extension.ini文件来。
箭头指向的是自己创建的ini文件,里面的内容是:
[cocos2dx_test]
# the prefix to be added to the generated functions. You might or might not use this in your own
# templates
prefix = cocos2dx_test
# 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.8/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.9/include
android_flags = -D_SIZE_T_DEFINED_
clang_headers = -I%(clangllvmdir)s/%(clang_include)s
clang_flags = -nostdinc -x c++ -std=c++11 -U __SSE__
cocos_headers = -I%(cocosdir)s -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/platform/android -I%(cocosdir)s/external/sources
cocos_flags = -DANDROID
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/test/Test.h
replace_headers = Test.h::test/Test.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 = Test
# what should we skip? in the format ClassName::[function function]
# 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
# classes that create no constructor
# Set is special and we will use a hand-written constructor
abstract_classes =
classes_need_extend =
文件内容的参数可以参考http://www.cocoachina.com/bbs/read.php?tid=177904
至于我们配置的.ini文件与原来的cocos2dx_extension.ini的不同之处,可以两个文件对比查看。
接着,打开genbindings.py文件,增加如下的一行:
打开终端,cd到genbindings.py文件所在的目录,运行./genbindings.py。
如无意外,并不成功。。。
但是只要一报错,就查看锦囊,就能逢凶化吉。
记住,只要一报错,就看一下。
直到出现
----------------------------------------
Generating javascript bindings succeeds.
----------------------------------------
为止。这时候在cocos/scripting/js-bindings/auto,出现在jsb_cocos2dx_test.cpp。
最后,我们打开jsb_module_register.cpp文件下添加如下代码:
和
至此,c++部分完整工作。
之后,写javascript测试: