Js Binding 实现

cocos官网最近更新,很多教程都没有了,这让本来就少的js资料,更少了。。。
这里是3.x版本的jsbinding,测试版本3.12,理论上3.x都会支持没,不负任何责任,有问题欢迎留言交流

参考文章:http://www.cocos2d-x.org/wiki/Binding_Custom_Class_To_Js_Runtime

自动绑定

bindings-generator, 它可以生成相对应的C++的绑定文件和相对应的JS接口,届时,你只需要调用生成的相对应的JS接口,它将自动转换成原来的C++的类,相当于直接调用原来的C++方法或者类。

用到的工具

1、cocos引擎
2、python2.7 ps:python版本必须是32位
3、py-ymal
4、cheetah
(关于3和4的安装,自行google)
5、Android NDK 版本一定要是r9b,不管它cocos版本要求的是什么版本的ndk,这里一定要用r9b,不然不成功!!!!!切记!!!!

需要手动拷贝引擎里的bindings-generator文件夹到项目的对应目录下

按照上面的文档翻译为中文如下:(win32下测试正常,原文环境是mac,估计推测应该win和mac都支持)

1、按照正常的方法编写需要绑定的C++类,这里使用测试类,位置: 项目根目录\framework\runtime-src\Classes\my\CustomClass.h & .cpp, 内容如下:

#pragma once
// 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";
}

2、在 项目根目录\frameworks\cocos2d-x\tools\tojs 下模仿cocos2dx.ini新建文件,cocos2dx_custom.ini,内容如下:

[cocos2dx_custom]
# 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 = tt

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 -U__SSE__

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/../runtime-src/Classes/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::[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 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

对于ini配置文件,这里我们修改了以下内容:
1).文件第1行中括号内的:cocos2dx_ui 修改为:cocos2dx_custom
2).prefix = cocos2dx_ui 修改为:prefix = cocos2dx_custom
3).target_namespace = ccui 修改为:target_namespace = tt
4).headers = (太长了这里省略) 修改为:headers=%(cocosdir)s/../runtime-src/Classes/my/CustomClass.h
这里的位置是根据自己的实际位置去填写,%(cocosdir)s的位置就是项目根目录frameworks\cocos2d-x目录
5).classes = CustomClass.*
这里等号后面的等于自己的C++类的名称

3、在ini文件同级目录下有一个genbindings.py文件,打开之后在custom_cmd_args = {}括号里面填上

'cocos2dx_custom.ini' : ('cocos2dx_custom', 'jsb_cocos2dx_custom'),

这里我会把文件中, cmd_args = {….}括号里面的内容注释掉,这样不会重新生成cocos本身的绑定文件,以节约时间。
4,在此目录下,cmd执行genbindings.py文件,如果不出错,会提示:

generating userconf.ini...
Generating bindings for cocos2dx_custom...
Using userconfig
  [('androidndkdir', 'D:\\CocosEnvironment\\android-ndk-r9b'), ('clangllvmdir', 'D:\\CocosEnvironment\\android-ndk-r9b\\toolchains\\llvm-3.3\\prebuilt\\windows-x86_64'), ('cocosdir', 'C:\\Users\\guangbao\\Desktop\\test\\frameworks\\cocos2d-x'), ('jsbdir', 'C:\\Users\\guangbao\\Desktop\\test\\frameworks\\cocos2d-x\\cocos\\scripting\\js-bindings'), ('cxxgeneratordir', 'C:\\Users\\guangbao\\Desktop\\test\\frameworks\\cocos2d-x\\tools\\bindings-generator'), ('extra_flags', '-D__WCHAR_MAX__=0x7fffffff -U__MINGW32__'), ('clang_version', '3.3')]

.... Generating bindings for target spidermonkey

.... .... Processing section cocos2dx_custom

----------------------------------------
Generating javascript bindings succeeds.
----------------------------------------

表示生成绑定文件成功。
5、用vs打开我们的工程,打开解决方案libjscocos2d,在auto目录下导入刚刚生成的两个文件,文件目录在 项目根目录\frameworks\cocos2d-x\cocos\scripting\js-bindings\auto下,名为jsb_cocos2dx_custom.cpp&jsb_cocos2dx_custom.hpp
这里可能会提示“无法打开包括文件CustomClass.h”,只需在解决方案libjscocos2d上右击->属性->C/C++->附加包含目录,打开之后添加新的目录:$(EngineRoot)..\runtime-src\Classes\my即可
6,在AppDelegate.cpp文件中添加以下代码:


#include "scripting/js-bindings/auto/jsb_cocos2dx_custom.hpp"

....

sc->addRegisterCallback(register_all_cocos2dx_custom);
....

7,在js代码中调用:

 var cla = tt.CustomClass.create();
        var msg = cla.helloMsg();
        cc.log("msg = " + msg);

然后vs运行项目,控制台输出:JS: msg = Hello from CustomClass::sayHello
就表示已经成功了

注意:编写ini文件的时候,clang_flags = …. 最后一定要加上 -U__SSE__,否则会报错:details = “use of undeclared identifier ‘_aligned_malloc’”>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
QML绑定是一种在QML中使用的机制,用于在不同的属性、对象和表达式之间建立动态关联。它允许我们在属性变化时自动更新相关的内容,以实现响应式编程。 QML绑定使用了JavaScript的语法和特性,它可以在声明式的QML代码中直接使用。通过使用绑定,我们可以将属性、变量、函数和其他QML对象连接在一起,以实现数据的同步更新。当绑定的其中一个部分发生变化时,其他绑定的部分也会自动更新。 例如,我们可以有一个简单的矩形对象,它具有width和height属性。我们可以将这两个属性绑定在一起,使得当宽度发生变化时,高度也会自动更新以保持相同的长宽比。这样,我们不需要手动编写代码来处理这个逻辑,而是将其交由绑定机制来自动完成。 在QML中,我们可以使用bind关键字来创建绑定关系,也可以使用更直观的属性绑定语法。通过绑定,我们可以连接QML对象的属性到其他属性,甚至是C++对象的属性,以实现更复杂的逻辑和动态交互。 绑定机制是QML中非常重要的一个特性,它使得我们能够以一种简单、易读的方式构建交互式的用户界面。通过合理使用绑定,我们可以降低代码的复杂性,提高代码的可维护性和可重用性。同时,绑定还能够提高用户体验,使得界面的更新和交互更加流畅和自然。 总之,QML绑定是一种强大而灵活的机制,它在QML编程中扮演了重要的角色。通过使用绑定,我们可以轻松地实现属性之间的关联和交互,并以一种声明式的方式构建复杂的用户界面。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值