Manual binding implementation of cocos2d-x-3.x version Js binding

Source: Internet
Author: User

Online circulating is a 2.x version, has now been updated to 3.x, after arduous, finally debugging success.

1. First define the class to be bound

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;}

Defining classes in the LS namespace, we test the Functest method

2, add the binding code to the appdelegate class

inside the Applicationdidfinishlaunching method, add Sc->addregistercallback (Register_all_ls); // classes that are manually bound

Write jsb_ls_auto.h , as follows

#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, uint32_t argc, Jsval *VP); bool  js_cocos2dx_animationkoofunctest_getdescription (Jscontext *cx, uint32_t argc, Jsval *VP); bool  js_cocos2dx_animationkoo_constructor (Jscontext *cx, uint32_t argc, Jsval *VP); void< C6/>register_all_ls (jscontext* cx, js::handleobject obj);

Yes, that's what it's like to write so many things.

Here's how it's implemented

Jsb_ls_auto.cpp


#include "cocos2d.h" #include "koogame/animationkoo.h" #include "jsapi.h" #include "jsb_ls_auto.h" #include "cocos2d_ SPECIFICS.HPP "//define the type of JS end Jsclass *jsb_lsleafsoar_class; Jsobject *jsb_lsleafsoar_prototype;//implements class binding under the ls namespace void Register_all_ls (jscontext* cx, Js::handleobject obj) {JS::Ro    Otedobject NS (CX);    Get_or_create_js_obj (CX, obj, "ls", &ns); Implement the Binding Leafsoar class, which defines the Js_register_ls_animationkoo (CX, NS) as defined below;} void Js_cocos2d_animationkoo_finalize (Jsfreeop *fop, Jsobject *obj) {ccloginfo ("Jsbindings:finalizing JS object%p (N Ode) ", obj);} BOOL Js_cocos2dx_animationkoo_create (Jscontext *cx, uint32_t argc, Jsval *vp) {Js::callargs args = JS::CALLARGSFROMVP (a    RGC, 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::animationk            oo*) 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, uint32_t argc, Jsval *vp) {Js::callargs args = JS::CALLARGSFROMVP (argc, VP)    ;    Args.rval (). SetBoolean (True);    return true; }bool js_cocos2dx_animationkoofunctest_getdescription (Jscontext *cx, uint32_t argc, Jsval *vp) {JS::CallArgs args = js:    : CALLARGSFROMVP (ARGC, 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, uint32_t argc, Jsval *vp) {Js::callargs args = Js::callargsfro    MVp (ARGC, 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, Typeclass->jsclass, 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, obj, "_ctor", &ok) && OK) scriptingcore::getinstance ()->executefunctionwithow    NER (Object_to_jsval (obj), "_ctor", 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, 0, JSPROP_PERM Anent |    jsprop_enumerate), js_fs_end}; Static Jsfunctionspec st_funcs[] = {JS_FN ("create", Js_cocos2dx_animationkoo_create, 0, Jsprop_permanent |    jsprop_enumerate), js_fs_end}; Jsb_lsleafsoar_prototype = Js_initclass (CX, Global, js::nullptr (),//parent Proto JSB_LSLEAFSOAR_CL Js_cocos2dx_animationkoo_constructor, 0,//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_setpropertya Ttributes (CX, Global, "Node", Jsprop_enumerate |    Jsprop_readonly, &found);    Add the proto and Jsclass to the TYPE-&GT;JS info hash tabletypetest<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_t        ype_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)); }}

This is going to be a lot more.

is one step away from success. Cocos2d JS in test

var Animationkoo =ls. Animationkoo.create ();

Animationkoo.functest ();

Output binding test ... , Successful


Manual binding implementation of cocos2d-x-3.x version Js binding

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.