网上流传的是一份2.x版本的,现在已更新到3.x,经过千辛万苦,终于调试成功。
1 .首先定义待绑定的类
AnimationKoo.h#ifndef __AnimationKoo_H__#define __AnimationKoo_H__namespace ls{ class AnimationKoo{ public: virtual voID functest(); static AnimationKoo * create(); };}#endif // __AnimationKoo_H__AnimationKoo.cpp#include "cocos2d.h"#include "cocos2d_specifics.hpp"#include " AnimationKoo.h"voID ls::AnimationKoo::functest(){ cclOG("binding test...");}ls::AnimationKoo * ls::AnimationKoo::create(){ AnimationKoo * ret = new AnimationKoo(); return ret;}
定义类在ls命名空间,我们测试funcTest方法
2,添加绑定代码到AppDelegate的类
在applicationDIDFinishLaunching方法里面,添加sc->addRegisterCallback(register_all_ls);//手动绑定的类
编写Jsb_ls_auto.h,如下
#include "JsAPI.h"#include "JsfrIEndAPI.h"#include "Scriptingcore.h"voID Js_register_ls_Animationkoo(jscontext* cx,Js::HandleObject global);voID Js_cocos2d_Animationkoo_finalize(JsFreeOp *fop,JsObject *obj);bool Js_cocos2dx_Animationkoo_create(jscontext *cx,uint32_t argc,Jsval *vp);static bool Js_is_native_obj(jscontext *cx,Jsval *vp);bool Js_cocos2dx_AnimationkooFuncTest_getDescription(jscontext *cx,Jsval *vp);bool Js_cocos2dx_Animationkoo_constructor(jscontext *cx,Jsval *vp);voID register_all_ls(jscontext* cx,Js::HandleObject obj);
没错,就是这么坑爹,需要写这么多东西。
下面是它的实现
Jsb_ls_auto.cpp
#include "cocos2d.h"#include "koogame/Animationkoo.h"#include "JsAPI.h"#include "Jsb_ls_auto.h"#include "cocos2d_specifics.hpp"// 定义 Js 端的类型JsClass *Jsb_LsLeafsoar_class;JsObject *Jsb_LsLeafsoar_prototype;// 实现 ls 命名空间下的类绑定voID register_all_ls(jscontext* cx,Js::HandleObject obj) { Js::Rootedobject ns(cx); get_or_create_Js_obj(cx,obj,"ls",&ns); // 实现绑定 Leafsoar 类,它的定义后文给出 Js_register_ls_Animationkoo(cx,ns);}voID Js_cocos2d_Animationkoo_finalize(JsFreeOp *fop,JsObject *obj) { cclOGINFO("Jsbindings: finalizing Js object %p (Node)",obj);}bool Js_cocos2dx_Animationkoo_create(jscontext *cx,Jsval *vp){ Js::CallArgs args = Js::CallArgsFromVp(argc,vp); if (argc == 0) { ls::AnimationKoo* ret = ls::AnimationKoo::create(); Jsval Jsret = JsVAL_NulL; do { if (ret) { Js_proxy_t *JsProxy = Js_get_or_create_proxy<ls::AnimationKoo>(cx,(ls::AnimationKoo*)ret); Jsret = OBJECT_TO_JsVAL(JsProxy->obj); } else { Jsret = JsVAL_NulL; } } while (0); args.rval().set(Jsret); return true; } Js_ReportError(cx,"Js_cocos2dx_Animationkoo_create : wrong number of arguments"); return false;}static bool Js_is_native_obj(jscontext *cx,vp); args.rval().setBoolean(true); return true; }bool Js_cocos2dx_AnimationkooFuncTest_getDescription(jscontext *cx,vp); Js::Rootedobject obj(cx,args.thisv().toObjectOrNull()); Js_proxy_t *proxy = Jsb_get_Js_proxy(obj); ls::AnimationKoo* cobj = (ls::AnimationKoo *)(proxy ? proxy->ptr : NulL); JsB_PRECONDITION2( cobj,cx,false,"Js_cocos2dx_Node_getDescription : InvalID Native Object"); if (argc == 0) { cobj->functest(); /* std::string ret = cobj->functest(); Jsval Jsret = JsVAL_NulL; Jsret = std_string_to_Jsval(cx,ret); args.rval().set(Jsret); */ return true; } Js_ReportError(cx,"Js_cocos2dx_Node_getDescription : wrong number of arguments: %d,was expecting %d",argc,0); return false;}bool Js_cocos2dx_Animationkoo_constructor(jscontext *cx,vp); bool ok = true; ls::AnimationKoo* cobj = new (std::nothrow) ls::AnimationKoo(); cocos2d::Ref *_ccobj = dynamic_cast<cocos2d::Ref *>(cobj); if (_ccobj) { _ccobj->autorelease(); } TypeTest<ls::AnimationKoo> t; Js_type_class_t *typeClass = nullptr; std::string typename = t.s_name(); auto typeMAPIter = _Js_global_type_map.find(typename); CCASSERT(typeMAPIter != _Js_global_type_map.end(),"Can't find the class type!"); typeClass = typeMAPIter->second; CCASSERT(typeClass,"The value is null."); // JsObject *obj = Js_NewObject(cx,typeClass->Jsclass,typeClass->proto,typeClass->parentProto); Js::Rootedobject proto(cx,typeClass->proto.get()); Js::Rootedobject parent(cx,typeClass->parentProto.get()); Js::Rootedobject obj(cx,Js_NewObject(cx,proto,parent)); args.rval().set(OBJECT_TO_JsVAL(obj)); // link the native object with the JavaScript object Js_proxy_t* p = Jsb_new_proxy(cobj,obj); AddnamedobjectRoot(cx,&p->obj,"ls::AnimationKoo"); if (Js_HasProperty(cx,"_ctor",&ok) && ok) Scriptingcore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JsVAL(obj),args); return true;}voID Js_register_ls_Animationkoo(jscontext* cx,Js::HandleObject global){ Jsb_LsLeafsoar_class = (JsClass *)calloc(1,sizeof(JsClass)); Jsb_LsLeafsoar_class->name = "AnimationKoo"; Jsb_LsLeafsoar_class->addProperty = Js_PropertyStub; Jsb_LsLeafsoar_class->delProperty = Js_DeletePropertyStub; Jsb_LsLeafsoar_class->getProperty = Js_PropertyStub; Jsb_LsLeafsoar_class->setProperty = Js_StrictPropertyStub; Jsb_LsLeafsoar_class->enumerate = Js_EnumerateStub; Jsb_LsLeafsoar_class->resolve = Js_ResolveStub; Jsb_LsLeafsoar_class->convert = Js_ConvertStub; Jsb_LsLeafsoar_class->finalize = Js_cocos2d_Animationkoo_finalize; Jsb_LsLeafsoar_class->flags = JsCLASS_HAS_RESERVED_SLOTS(2); static JsPropertySpec propertIEs[] = { Js_PSG("__nativeObj",Js_is_native_obj,JsPROP_PERMANENT | JsPROP_ENUMERATE),Js_PS_END }; static JsFunctionspec funcs[] = { Js_FN("funcTest",Js_cocos2dx_AnimationkooFuncTest_getDescription,Js_FS_END }; static JsFunctionspec st_funcs[] = { Js_FN("create",Js_cocos2dx_Animationkoo_create,Js_FS_END }; Jsb_LsLeafsoar_prototype = Js_InitClass( cx,global,Js::NullPtr(),// parent proto Jsb_LsLeafsoar_class,Js_cocos2dx_Animationkoo_constructor,// constructor propertIEs,funcs,NulL,// no static propertIEs st_funcs); // make the class enumerable in the registered namespace// bool found;//FIXME: Removed in firefox v27 // Js_SetPropertyAttributes(cx,"Node",JsPROP_ENUMERATE | JsPROP_Readonly,&found); // add the proto and JsClass to the type->Js info hash table TypeTest<ls::AnimationKoo> t; Js_type_class_t *p; std::string typename = t.s_name(); if (_Js_global_type_map.find(typename) == _Js_global_type_map.end()) { p = (Js_type_class_t *)malloc(sizeof(Js_type_class_t)); p->Jsclass = Jsb_LsLeafsoar_class; p->proto = Jsb_LsLeafsoar_prototype; p->parentProto = NulL; _Js_global_type_map.insert(std::make_pair(typename,p)); }}
这次要写的更多了。
离成功就差一步了。cocos2d Js中测试
var animationKoo =ls.AnimationKoo.create();
animationKoo.functest();
输出binding test...,成功
总结以上是内存溢出为你收集整理的Cocos2d-x-3.x版 Js Binding 的手动绑定实现全部内容,希望文章能够帮你解决Cocos2d-x-3.x版 Js Binding 的手动绑定实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)