cocos2d中间层创建(一)

前端之家收集整理的这篇文章主要介绍了cocos2d中间层创建(一)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先编辑jsb中间层,主要是在AppDelegate中进行注册单例,在js中进行调用

jsb_lcm_api.h:

//
//  jsb_lcm_api.h
//  MoonWarriors
//
//  Created by xiaohei on 2/21/17.
//
//

#ifndef __LCM__jsb_lcm_api__
#define __LCM__jsb_lcm_api__

#include "jsapi.h"
#include "jsfriendapi.h"
#include "js_bindings_lcm_api.h"

extern JSClass  *js_cocos2dx_LCMAPI_class;
extern JSObject *js_cocos2dx_LCMAPI_prototype;
///
/// @brief LCM API
///
void js_register_LCMAPI(JSContext* cx,JSObject* global );

void register_all_LCMAPI(JSContext* cx,JSObject* obj);

#endif /* defined(__LCM__jsb_lcm_api__) */


jsb_lcm_api.cpp:
//
//  jsb_lcm_api.cpp
//  MoonWarriors
//
//  Created by xiaohei on 2/21/17.
//
//

#include "jsb_lcm_api.h"

///
/// @brief  JS Internal Header Files
///
#include "jsapi.h"
#include "ScriptingCore.h"
#include "cocos2d_specifics.hpp"
#include "cocos2d.h"
#include "AppDelegate.h"

static bool empty_constructor(JSContext *cx,uint32_t argc,jsval *vp) {
	return false;
}

JSClass  *js_cocos2dx_LCMAPI_class;
JSObject *js_cocos2dx_LCMAPI_prototype;
extern JSObject *jsb_cocos2d_ActionInterval_prototype;

bool js_cocos2dx_extension_LCMAPI_getLTotalBalance(JSContext *cx,jsval *vp)
{
  jsval *argv = JS_ARGV(cx,vp);
 
  JSObject *obj = JS_THIS_OBJECT(cx,vp);
  js_proxy_t *proxy = jsb_get_js_proxy(obj);
  LCMJSB::LCMAPI* cobj = (LCMJSB::LCMAPI *)(proxy ? proxy->ptr : NULL);
  JSB_PRECONDITION2( cobj,cx,false,"Invalid Native Object");
 
  if (argc == 0)
  {
    std::string ret = cobj->getLTotalBalance();
    jsval jsret = std_string_to_jsval(cx,ret);
    JS_SET_RVAL(cx,vp,jsret);
    return true;
  }
 
  JS_ReportError(cx,"wrong number of arguments: %d,was expecting %d",argc,0);
  return false;
}

bool js_cocos2dx_extension_LCMAPI_lcmNotifyEveryday(JSContext *cx,jsval *vp)
{
    jsval *argv = JS_ARGV(cx,vp);
    
    JSObject *obj = JS_THIS_OBJECT(cx,vp);
    js_proxy_t *proxy = jsb_get_js_proxy(obj);
    LCMJSB::LCMAPI* cobj = (LCMJSB::LCMAPI *)(proxy ? proxy->ptr : NULL);
    JSB_PRECONDITION2( cobj,"Invalid Native Object");
    
    if (argc == 6)
    {
        int hour = JSVAL_TO_INT(argv[0]);
        int min = JSVAL_TO_INT(argv[1]);
        JSString *str = JSVAL_TO_STRING(argv[2]);
        JSStringWrapper strWrap(str);
        std::string title = strWrap.get();
        JSString *str2 = JSVAL_TO_STRING(argv[3]);
        JSStringWrapper strWrap2(str2);
        std::string txt = strWrap2.get();
        JSString *str3 = JSVAL_TO_STRING(argv[4]);
        JSStringWrapper strWrap3(str3);
        std::string icon = strWrap3.get();
        int code = JSVAL_TO_INT(argv[5]);
        cobj->lcmNotifyEveryday(hour,min,title,txt,icon,code);
        JS_SET_RVAL(cx,JSVAL_VOID);
        return true;
    }
    
    JS_ReportError(cx,6);
    return false;
}

void js_register_LCMAPI(JSContext *cx,JSObject *global) {
    
    js_cocos2dx_LCMAPI_class              = (JSClass *)calloc(1,sizeof(JSClass));
    js_cocos2dx_LCMAPI_class->name        = "LCMAPI";
    js_cocos2dx_LCMAPI_class->addProperty = JS_PropertyStub;
    js_cocos2dx_LCMAPI_class->delProperty = JS_DeletePropertyStub;
    js_cocos2dx_LCMAPI_class->getProperty = JS_PropertyStub;
    js_cocos2dx_LCMAPI_class->setProperty = JS_StrictPropertyStub;
    js_cocos2dx_LCMAPI_class->enumerate   = JS_EnumerateStub;
    js_cocos2dx_LCMAPI_class->resolve     = JS_ResolveStub;
    js_cocos2dx_LCMAPI_class->convert     = JS_ConvertStub;
    js_cocos2dx_LCMAPI_class->finalize    = js_cocos2dx_LCMAPI_finalize;
    js_cocos2dx_LCMAPI_class->flags       = JSCLASS_HAS_RESERVED_SLOTS(4);
    
    static JSPropertySpec properties[] = {
		{0,JSOP_NULLWRAPPER,JSOP_NULLWRAPPER}
    };
    
    static JSFunctionSpec funcs[] = {
        JS_FN("continueProcessing",js_cocos2dx_extension_LCMAPI_continueProcessing,JSPROP_PERMANENT | JSPROP_ENUMERATE),JS_FN("LUpdateBalance",js_cocos2dx_extension_LCMAPI_LUpdateBalance,JS_FN("checkBalanceEnoughForPay",js_cocos2dx_extension_LCMAPI_checkBalanceEnoughForPay,2,JS_FN("setLExtraData",js_cocos2dx_extension_LCMAPI_setLExtraData,JS_FN("getLAccessToken",js_cocos2dx_extension_LCMAPI_getLAccessToken,JS_FN("getLUserStringInfo",js_cocos2dx_extension_LCMAPI_getLUserStringInfo,JS_FN("getLIdFromUser",js_cocos2dx_extension_LCMAPI_getLIdFromUser,JS_FN("getLTotalBalance",js_cocos2dx_extension_LCMAPI_getLTotalBalance,JS_FN("getLPaidBalance",js_cocos2dx_extension_LCMAPI_getLPaidBalance,JS_FN("getLFreeBalance",js_cocos2dx_extension_LCMAPI_getLFreeBalance,JS_FN("getLIsShowBalance",js_cocos2dx_extension_LCMAPI_getLIsShowBalance,JS_FN("setLMenuBarVisible",js_cocos2dx_extension_LCMAPI_setLMenuBarVisible,JS_FN("showLCustomerService",js_cocos2dx_extension_LCMAPI_showLCustomerService,JS_FN("lcmlogout",js_cocos2dx_extension_LCMAPI_lcmlogout,JS_FN("lcmLogin",js_cocos2dx_extension_LCMAPI_lcmLogin,JS_FN("lcmNotifyClean",js_cocos2dx_extension_LCMAPI_lcmNotifyClean,JS_FN("lcmNotifyEveryday",js_cocos2dx_extension_LCMAPI_lcmNotifyEveryday,6,JS_FS_END
    };
    
	static JSFunctionSpec st_funcs[] = {
		JS_FN("getInstance",js_cocos2dx_extension_LCMAPI_getInstance,JS_FS_END
	};
    
    js_cocos2dx_LCMAPI_prototype = JS_InitClass(
                                                   cx,global,NULL,js_cocos2dx_LCMAPI_class,empty_constructor,// constructor
                                                   properties,funcs,// no static properties
                                                   st_funcs);
    
			TypeTest<LCMJSB::LCMAPI> 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 = js_cocos2dx_LCMAPI_class;
				p->proto = js_cocos2dx_LCMAPI_prototype;
				p->parentProto = jsb_cocos2d_ActionInterval_prototype;
				_js_global_type_map.insert(std::make_pair(typeName,p));
			}

}

void register_all_LCMAPI(JSContext* cx,JSObject* obj)
{
	JS::RootedValue nsval(cx);
	JS::RootedObject ns(cx);
	
	JS_GetProperty(cx,obj,"LCMJSB",&nsval);
	if (nsval == JSVAL_VOID) {
		ns = JS_NewObject(cx,NULL);
		nsval = OBJECT_TO_JSVAL(ns);
		JS_SetProperty(cx,nsval);
	} else {
		JS_ValueToObject(cx,nsval,&ns);
	}
	obj = ns;
	
	js_register_LCMAPI(cx,obj);
}

AppDelegate.cpp 中进行注册
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "../SDK/lcm9.3/LCMJS/jsb_lcm_api.h"
#include "../SDK/lcm9.3/LCMJS/js_bindings_lcm_api.h"
#endif

ScriptingCore* sc = ScriptingCore::getInstance();
 
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
    //lcm
    sc->addRegisterCallback(register_all_LCMAPI);
#endif


js中进行调用
if (g_targetOS != TargetOS.WINDOWS && g_targetOS != TargetOS.MACOS && MobageTool.isAvailable()) {
    LCMJSB.LCMAPI.getInstance().lcmNotifyClean();
    LCMJSB.LCMAPI.getInstance().lcmNotifyEveryday(12,__getClientMsg(371),client_msg_hash[354].n,"icon",1);
    LCMJSB.LCMAPI.getInstance().lcmNotifyEveryday(18,client_msg_hash[356].n,2);
}
原文链接:https://www.f2er.com/cocos2dx/339266.html

猜你在找的Cocos2d-x相关文章