cocos2d中间层创建(一)

原创 2016年08月30日 10:30:04

首先编辑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, uint32_t argc, 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, uint32_t argc, 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 == 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, vp, JSVAL_VOID);
        return true;
    }
    
    JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 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, 0, 0, JSOP_NULLWRAPPER, JSOP_NULLWRAPPER}
    };
    
    static JSFunctionSpec funcs[] = {
        JS_FN("continueProcessing",js_cocos2dx_extension_LCMAPI_continueProcessing, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("LUpdateBalance",js_cocos2dx_extension_LCMAPI_LUpdateBalance, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("checkBalanceEnoughForPay",js_cocos2dx_extension_LCMAPI_checkBalanceEnoughForPay, 2, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("setLExtraData",js_cocos2dx_extension_LCMAPI_setLExtraData, 2, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("getLAccessToken",js_cocos2dx_extension_LCMAPI_getLAccessToken, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("getLUserStringInfo",js_cocos2dx_extension_LCMAPI_getLUserStringInfo, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("getLIdFromUser",js_cocos2dx_extension_LCMAPI_getLIdFromUser, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("getLTotalBalance",js_cocos2dx_extension_LCMAPI_getLTotalBalance, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("getLPaidBalance",js_cocos2dx_extension_LCMAPI_getLPaidBalance, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("getLFreeBalance",js_cocos2dx_extension_LCMAPI_getLFreeBalance, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("getLIsShowBalance",js_cocos2dx_extension_LCMAPI_getLIsShowBalance, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("setLMenuBarVisible",js_cocos2dx_extension_LCMAPI_setLMenuBarVisible, 2, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("showLCustomerService",js_cocos2dx_extension_LCMAPI_showLCustomerService, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("lcmLogout",js_cocos2dx_extension_LCMAPI_lcmLogout, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("lcmLogin",js_cocos2dx_extension_LCMAPI_lcmLogin, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("lcmNotifyClean",js_cocos2dx_extension_LCMAPI_lcmNotifyClean, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("lcmNotifyEveryday",js_cocos2dx_extension_LCMAPI_lcmNotifyEveryday, 6, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FS_END
    };
    
	static JSFunctionSpec st_funcs[] = {
		JS_FN("getInstance", js_cocos2dx_extension_LCMAPI_getInstance, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
		JS_FS_END
	};
    
    js_cocos2dx_LCMAPI_prototype = JS_InitClass(
                                                   cx, global,
                                                   NULL,
                                                   js_cocos2dx_LCMAPI_class,
                                                   empty_constructor, 0, // constructor
                                                   properties,
                                                   funcs,
                                                   NULL, // 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, NULL, NULL);
		nsval = OBJECT_TO_JSVAL(ns);
		JS_SetProperty(cx, obj, "LCMJSB", 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,0,__getClientMsg(371),client_msg_hash[354].n,"icon",1);
    LCMJSB.LCMAPI.getInstance().lcmNotifyEveryday(18,0,__getClientMsg(371),client_msg_hash[356].n,"icon",2);
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

cocos2d中间层调用Android(三)

ios通过.cpp文件进行调用AppActivity.java的方法 js_bindings_lcm_api.cpp: // // js_bindings_lcm_api.cpp // Moo...

cocos2d项目创建

  • 2017年06月18日 00:24
  • 233B
  • 下载

cocos2d-android学习三 ---- 创建布景层

上篇文章讲了怎么下载

cocos2d创建工程

  • 2016年02月19日 10:37
  • 689B
  • 下载

Unity3D研究之在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信引入中间层

 一对多的观察者模式机制有什么缺点? 如果你对如何在Unity中使用事件/委托机制还不太了解,建议您查看我的前一篇文章:在Unity中使用事件/委托机制(event/delegate)进行...

【Unity3D技巧】在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信 (二) : 引入中间层NotificationCenter

转载自:http://www.cnblogs.com/neverdie/p/3790879.html?utm_source=tuicool&utm_medium=referral 作者:王选易,...

cocos2d-js复制图层图像

  • 2017年11月04日 15:34
  • 1KB
  • 下载

cocos2d-js 创建工程

Cocos2d-JS最引以为傲的能力就是完美的一次开发,全平台部署得能力。得益于3.0版中Web引擎和Native引擎的整合,Cocos2d-JS为Web和各原生平台开发提供了一套统一的工作流,开发者...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:cocos2d中间层创建(一)
举报原因:
原因补充:

(最多只允许输入30个字)