定制引擎-标题不够吸引人么?

Creator版本:1.8
工作环境:mac

问题描述

:js自动绑定了一个自己实现的CPP类 jsbTest,自动绑定结果如下

bool js_register_jsbTest_jsbTest(se::Object* obj)
{
//暴露cpp类到JS中
auto cls = se::Class::create(“jsbTest”, obj, nullptr, nullptr);

cls->defineFunction("testlog", _SE(js_jsbTest_jsbTest_testlog));
cls->defineFinalizeFunction(_SE(js_jsbTest_finalize));
cls->install();
JSBClassType::registerClass<jsbTest>(cls);
__jsb_jsbTest_proto = cls->getProto();
__jsb_jsbTest_class = cls;
se::ScriptEngine::getInstance()->clearException();
return true;

}

bool register_all_jsbTest(se::Object* obj)
{
// Get the global ns
se::Object* ns = se::ScriptEngine::getInstance()->getGlobalObject();

js_register_jsbTest_jsbTest(ns);
return true;

}

在js代码中调用如下

// use this for initialization
onLoad: function () {
    this.label.string = this.text;
    var tmp = new jsbTest();
    tmp.testlog();
},

使用模拟器预览当前场景,报错:

Simulator: JS: [WARN]: Sorry, lineHeight of system font not supported on JSB.
Simulator: jsb: ERROR: File /Users/tuyou/tcz/study/cocos2d-x-lite/cocos/scripting/js-bindings/auto/jsb_jsbTest_auto.cpp: Line: 12, Function: js_jsbTest_jsbTest_testlog
Simulator: js_jsbTest_jsbTest_testlog : Invalid Native Object
Simulator: [ERROR] Failed to invoke js_jsbTest_jsbTest_testlog, location: /Users/tuyou/tcz/study/cocos2d-x-lite/cocos/scripting/js-bindings/auto/jsb_jsbTest_auto.cpp:22
Simulator: JS: Activate: 130.57399999999961ms

打IOS包测试,跟cretor中预览结果一样

但是使用安卓打包,可以正常调用到。

cocos creator模拟器已重新生成
**

希望最终达成效果

**:cocos creator中也可以正常调用C++的绑定类

请熟悉这个的同学帮忙解答下该如何处理,谢谢~

:9:

@jare大大,求帮忙指点下

已实现。

经过一个周的摸索,终于知道怎么绑定了。
上面的代码,加一个构造函数就能在IOS、Creator中正确调用到C++了

static bool js_jsbTest_jsbTest_constructor(se::State& s)
{
jsbTest* cobj = new jsbTest();
s.thisObject()->setPrivateData(cobj);
return true;
}

SE_BIND_CTOR(js_jsbTest_jsbTest_constructor,__jsb_jsbTest_class,js_jsbTest_finalize)

bool js_register_jsbTest_jsbTest(se::Object* global)
{
//暴露cpp类到JS中

auto cls = se::Class::create("jsbTest",global,nullptr,_SE(js_jsbTest_jsbTest_constructor));

cls->defineFunction("testlog", _SE(js_jsbTest_jsbTest_testlog));
cls->defineProperty("ajs", _SE(js_jsbTest_a_get), _SE(js_jsbTest_a_set));
cls->defineFinalizeFunction(_SE(js_jsbTest_finalize));
cls->install();
JSBClassType::registerClass<jsbTest>(cls);
__jsb_jsbTest_proto = cls->getProto();
__jsb_jsbTest_class = cls;
se::ScriptEngine::getInstance()->clearException();
return true;

}

看起来不错, 试用一下

JSB 2.0 使用指南 · GitBook
http://docs.cocos.com/creator/manual/zh/advanced-topics/jsb/JSB2.0-learning.html

请问你注册到AppDelegate里是怎么写的,这个js_register_jsbTest_jsbTest怎么被调用的?可以看看你的吗

这里有个注测入口