1、http://www.cocos2d-x.org/docs/manual/code-ide/function-guides/add-3rd-party-lib/binding-custom-class-to-js/zh
2、http://cocos2d.9tech.cn/news/2014/0106/39499.html
4、添加自定义函数 :std::vector<Vec2> test(std::vector<Vec2> v){ return v;}5、添加自定义类型 std::vector<Vec2> (因为自带的没有该类型转换):
在tools/bindings-generator/targets/spidermonkey/conversions.yaml 中添加代码
找到native_types: 在下面添加一行
"@std::vector<cocos2d::Vec2.*>.>": "std::vector<cocos2d::Vec2>"找到 # int to jsval 在下面添加一行
"@vector<cocos2d::Vec2.*>": "${out_value} = std_vector_ccvec2_to_jsval(cx,${in_value})"
找到 # jsval to int 在下面添加一行"@vector<cocos2d::Vec2.*>": "ok &= jsval_to_std_vector_ccvec2(cx,${in_value},&${out_value})"
在frameworks/js-bindings/bindings/manual/js_manual_conversions中添加代码
jsval std_vector_ccvec2_to_jsval( JSContext *cx,const std::vector<cocos2d::Vec2>& v) { JS::RootedObject jsretArr(cx,JS_NewArrayObject(cx,0)); int i = 0; for (const cocos2d::Vec2 obj : v) { JS::RootedValue arrElement(cx); arrElement = vector2_to_jsval(cx,obj); if (!JS_SetElement(cx,jsretArr,i,arrElement)) { break; } ++i; } return OBJECT_TO_JSVAL(jsretArr); }
bool jsval_to_std_vector_ccvec2( JSContext *cx,JS::HandleValue vp,std::vector<cocos2d::Vec2>* ret){ JS::RootedObject jsobj(cx); bool ok = vp.isObject() && JS_ValueToObject( cx,vp,&jsobj ); JSB_PRECONDITION3( ok,cx,false,"Error converting value to object"); JSB_PRECONDITION3( jsobj && JS_IsArrayObject( cx,jsobj),"Object must be an array"); uint32_t len = 0; JS_GetArrayLength(cx,jsobj,&len); for (uint32_t i=0; i < len; i++) { JS::RootedValue value(cx); if (JS_GetElement(cx,&value)) { if (value.isObject()) { Vec2 v; ok = jsval_to_vector2(cx,value,&v); if (ok) { ret->push_back(static_cast<Vec2>(v)); } } else { JS_ReportError(cx,"not supported type in array"); return false; } } } return true; }5、以上可以在xcode上跑起来, 如果需要打包安卓包, 需要在Android.mk中添加
../../../js-bindings/cocos2d-x/cocos/user/SwimAlg.cpp\ ../../../custom/auto/jsb_SwimAlg.cpp \
$(LOCAL_PATH)/../../../js-bindings/cocos2d-x/cocos/user \原文链接:https://www.f2er.com/cocos2dx/340903.html