【H5+ & Quick-cocos2dx整合】之iOS 四 协调H5+和Quick-Cocos2dx之间工作

【H5+ & Quick-cocos2dx整合】之iOS 四 协调H5+和Quick-Cocos2dx之间工作,第1张

概述JavaScript和Lua两者分别调用Native OC接口通道,实现这两个框架的协调工作。 H5+ SDK以插件的实现客户化的调用Native OC,Lua则可以通过tolua工具实现。 开发H5+ SDK插件   插件名称:PGPluginTest PluginTest.h @interfacePGPluginTest : PGPlugin   - (void)gotoMyGame:(PGM

JavaScript和Lua两者分别调用Native OC接口通道,实现这两个框架的协调工作。

H5+ SDK以插件的实现客户化的调用Native OC,Lua则可以通过tolua工具实现。

开发H5+ SDK插件

插件名称:PGPluginTest

PluginTest.h

@interfacePGPluginTest : PGPlugin

- (voID)gotoMyGame:(pgmethod*)commands;

+ (voID) back2App;

@end

PluginTest.mm

@implementationPGPluginTest

- (voID)gotoMyGame:(pgmethod*)commands

{

Nsstring* cbID = [commands.arguments objectAtIndex:0];

// 用户的参数会在第二个参数传回

Nsstring* pArgument1 = [commands.arguments objectAtIndex:1];

NSLog(@"gotomygame>>>:%@",pArgument1);

Nsstring* pResultString = @"success";

// 运行Native代码结果和预期相同,调用回调通知Js层运行成功并返回结果

PDRPluginResult *result = [PDRPluginResult resultWithStatus:PDRCommandStatusOK messageAsstring:pResultString];

AppManager* mgr = [AppManager getInstance];

mgr.gamename = [Nsstring stringWithFormat:@"%@%@%@",@"games/",pArgument1,@"/src/main.lua" ];

mgr.ccVIEw = [[CocosVIEwController alloc] init];

mgr.h5VIEw = [[mgr.navigationController vIEwControllers] objectAtIndex:0];

[mgr.navigationController pushVIEwController:mgr.ccVIEw animated:YES];

[self toCallback:cbID withReslut:[result toJsONString]];

}

+ (voID) back2App

{

[[AppManager getInstance] releaseCocos];

}

实现C++ tolua

LuaHelper.mm

int LuaHelper::back2App()

{

cclOG("LuaHelper back2App");

[PGPluginTest back2App];

return 1;

}

GameUtil.h

#ifndef __GAME_UTIL_H_

#define __GAME_UTIL_H_

extern "C" {

#include "lua.h"

#include "tolua++.h"

}

#include "tolua_fix.h"

TolUA_API int luaopen_GameUtil_luabinding(lua_State* tolua_S);

#endif

GameUtil.cpp

#include "GameUtil.h"

#include "LuaHelper.h"

static voID tolua_reg_types (lua_State* tolua_S)

{

tolua_usertype(tolua_S,"LuaHelper");

}

#ifndef TolUA_disABLE_tolua_HelperFunc_luabinding_HelperFunc_getfileData00

static int tolua_LuaHelper_luabinding_LuaHelper_back2App00(lua_State* tolua_S)

{

#if COCOS2D_DEBUG >= 1

tolua_Error tolua_err;

if (

!tolua_isusertable(tolua_S,1,"LuaHelper",&tolua_err) ||

!tolua_isstring(tolua_S,2,&tolua_err) ||

!tolua_isnoobj(tolua_S,3,&tolua_err)

)

goto tolua_lerror;

else

#endif

{

LuaHelper::back2App();

}

return 1;

#if COCOS2D_DEBUG >= 1

tolua_lerror:

tolua_error(tolua_S,"#ferror in function 'back2App'.",&tolua_err);

return 0;

#endif

}

#endif

TolUA_API int tolua_GameUtil_luabinding_open (lua_State* tolua_S)

{

tolua_open(tolua_S);

tolua_reg_types(tolua_S);

tolua_cclass(tolua_S,"",NulL);

tolua_beginmodule(tolua_S,"LuaHelper");

tolua_function(tolua_S,"back2App",tolua_LuaHelper_luabinding_LuaHelper_back2App00);

tolua_endmodule(tolua_S);

return 1;

}

#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501

TolUA_API int luaopen_GameUtil_luabinding (lua_State* tolua_S) {

return tolua_GameUtil_luabinding_open(tolua_S);

};

#endif

AppDelegate.mm

quick_module_register方法调用luaopen_GameUtil_luabinding(L)以便于Lua调用C++接口

窗口管理

关系图如下:

AppManager.h:为了管理整个应用的所有UIWindow和UIController的加载和移除。

static AppManager *instance=nil;

@implementation AppManager

@synthesize gamename;

+ (AppManager*)getInstance{

static dispatch_once_toncetoken ;

dispatch_once(&oncetoken,^{

instance = [[super allocWithZone:NulL] init] ;

}) ;

return instance;

}

+(ID) allocWithZone:(struct _NSZone *)zone

{

return [AppManagerinstance] ;

}

-(ID) copyWithZone:(struct _NSZone *)zone

{

return [AppManagerinstance] ;

}

//释放cocos所有变量

- (voID)releaseCocos{

cocos2d::CCDirector* director = cocos2d::CCDirector::sharedDirector();

director->end();

[self.ccVIEw.window resignKeyWindow];

[self.ccVIEw.window removeFromSupervIEw];

self.ccVIEw.window.rootVIEwController = nil;

self.ccVIEw.window.hIDden=YES;

[self.ccVIEw.window release];

[self.navigationController popToVIEwController:self.h5VIEw animated:YES];

self.ccVIEw.window=nil;

self.ccVIEw=nil;

[self performSelector:@selector(releaseLuaEngine) withObject:nil afterDelay:0.1];

//[self.navigationController.parentVIEwController makeKeyAndVisible];

}

- (voID)releaseLuaEngine{

cocos2d::ScriptEngineManager::getInstance()->destroyInstance();

}

@end

总结

以上是内存溢出为你收集整理的【H5+ & Quick-cocos2dx整合】之iOS 四 协调H5+和Quick-Cocos2dx之间工作全部内容,希望文章能够帮你解决【H5+ & Quick-cocos2dx整合】之iOS 四 协调H5+和Quick-Cocos2dx之间工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1081132.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存