The cocos creator version 2.4.3 is used; directly paste the code, the structure is as follows: Create two files jsb_test.hpp and jsb_test.cpp
in the CocosDashboard\resources.editors\Creator\2.4.3\resources\cocos2d-x\cocos\scripting\js-bindings\manual folder . The content of jsb_test.hpp is as follows:
#pragma once
namespace se {
class Object;
}
bool register_all_testio(se::Object* obj);
The jsb_test.cpp code is as follows:
#include "jsb_test.hpp"
#include "cocos/scripting/js-bindings/manual/jsb_conversions.hpp"
static bool jsb_Test(se::State &s)
{
CC_UNUSED std::string ok = "";
std::string result = "这是一条测试";//这里可以调用其他的C++逻辑,简写了。
ok = std_string_to_seval(result, &s.rval()); //返回string
return true;
}
SE_BIND_FUNC(jsb_Test)
bool register_all_testio(se::Object *obj)
{
se::Value nsVal;
if (!obj->getProperty("Test", &nsVal))
{
se::HandleObject jsobj(se::Object::createPlainObject());
nsVal.setObject(jsobj);
obj->setProperty("Test", nsVal);
}
se::Object *ns = nsVal.toObject();
ns->defineFunction("test", _SE(jsb_Test));
//js中直接Test.test();调用
return true;
}
Then add the following code in CocosDashboard\resources.editors\Creator\2.4.3\resources\cocos2d-x\cocos\scripting\js-bindings\manual\jsb_module_register.cpp.
#include "cocos/scripting/js-bindings/manual/jsb_test.hpp"
// 注册模块
se->addRegisterCallback(register_all_testio);
The worst thing is that the CreatorDemo\build\jsb-link\frameworks\runtime-src\Classes\jsb_module_register.cpp of the corresponding project also needs to add the above code (take my project as an example, everyone should respond flexibly).
Add the corresponding cpp file to the CocosDashboard\resources.editors\Creator\2.4.3\resources\cocos2d-x\cocos\Android.mk file, generate so package and then use it.
LOCAL_SRC_FILES += \
scripting/js-bindings/manual/jsb_test.cpp
So far, the simple manual binding has been realized, and the complex one can be realized by referring to the manual.
https://docs.cocos.com/creator/manual/zh/advanced-topics/JSB2.0-learning.html
https://docs.cocos.com/creator/manual/zh/advanced-topics/jsb-manual-binding.html?h=jsb