文章主要參考:https://blog.csdn.net/xjw532881071/article/details/108715666 ,在原來基礎(chǔ)上,根據(jù)自己的集成經(jīng)驗(yàn)進(jìn)行了完善
1:獲取到Cocos Creator構(gòu)建的iOS工程
這一步主要是Cocos Creator開發(fā)工程師提供,最后給到我們的是構(gòu)建好的
jsb-default文件夾下的內(nèi)容就是我們iOS開發(fā)需要的代碼了
2.運(yùn)行jsb-default下的iOS工程
這一步的作用是為了驗(yàn)證構(gòu)建的工程是沒有問題的,同時(shí)獲取到我們這個(gè)工程集成到iOS需要的配置是哪些.
通過以上路勁,運(yùn)行iOS項(xiàng)目hello_world.xcodeproj
,進(jìn)行證書配置,然后采用真機(jī)運(yùn)行,如果能夠正常運(yùn)行,說明構(gòu)建的iOS工程是沒有問題的.
得到一個(gè)能夠正常運(yùn)行的工程非常重要,非常重要,非常重要!!!因?yàn)槲覀冊(cè)谧约旱膇OS工程中集成Cocos Creator游戲用到的配置信息都是從這個(gè)工程中是查找的.
3.真正的集成開始
3.1 導(dǎo)入游戲代碼到iOS原生工程
在工程目錄下創(chuàng)建 cocos
文件夾, 然后找到之前游戲構(gòu)建的代碼,復(fù)制到cocos
文件夾下,這里的jsb-default就是cocos項(xiàng)目構(gòu)建成功后的代碼:
注:frameworks中,可以只導(dǎo)入frameworks-->runtime-src-->proj.ios_mac + Classes ,
proj.android-studio
和proj.win32
可以不導(dǎo)入
3.2 導(dǎo)入游戲代碼到iOS原生工程
打開iOS原生工程,導(dǎo)入cocos下的文件和文件夾.這里要注意文件夾的黃色(group)和藍(lán)色(fold references)區(qū)別锋八。
以藍(lán)色fold references導(dǎo)入assets轰豆、cocos2d_libs.xcodeproj;
新建一個(gè)名為CocosResources的group泻蚊,并以fold references模式導(dǎo)入main.js怒炸、project.json参萄、src逃延、jsb-adapter到其中;
以黃色group模式導(dǎo)入ios拖叙、Classes文件夾;
注:如果沒有cocos2d_libs.xcodeproj,找cocos開發(fā)工程師構(gòu)建(就是構(gòu)建時(shí)勾選default,不是link)
結(jié)果如下:
3.3 工程中移除ios文件夾下的main.m
3.4 導(dǎo)入項(xiàng)目的依賴庫(kù)
這里我們就需要使用到之前運(yùn)行起來的cocos下生產(chǎn)的工程了,去里面找依賴庫(kù),我的如下:
把庫(kù)全部導(dǎo)入即可, .lib
的文件,直接采用tbd
代替;
3.5 導(dǎo)入libcocos2d iOS(cocos2d_libs)
為原生iOS工程的TARGETS添加Dependences(Build Phase/Dependencies):libcocos2d iOS(cocos2d_libs),有兩處都要添加
注:如果刪除了cocos下的文件夾,重新導(dǎo)入了,可能會(huì)導(dǎo)致 libcocos2d iOS丟失,需要再次按照上面的流程重新導(dǎo)入一次,不然報(bào)錯(cuò):
cocos的一些方法找不到了
;
3.6 工程配置
- 參考cocos導(dǎo)出的iOS工程為原生工程的PROJECT添加以下頭文件搜索路徑(Build Settings/Search Paths/User Header Search Paths), (SRCROOT)為相對(duì).xcodeproj文件的路徑,
Always Search User Paths
設(shè)為Yes
注:以上的
User Header Search Paths
中的配置要參考之前cocos導(dǎo)出的iOS工程,同時(shí)請(qǐng)驗(yàn)證路徑是否正確
,我的如下:
這里需要100%確認(rèn)正確,不然會(huì)出現(xiàn)一堆的問題;
參考cocos導(dǎo)出的iOS工程為原生工程的
TARGETS
添加以下頭文件(搜索路徑:Build Settings/Search Paths/User Header Search Paths
)同時(shí)把
Always Search User Paths
設(shè)為Yes;TARGETS
下,為Library Search Paths
添加:
$(SRCROOT)/cocos/frameworks/cocos2d-x/external/iOS/libs
cocos/frameworks/cocos2d-x/external/iOS/libs
這個(gè)路徑自己驗(yàn)證下,如果你創(chuàng)建的也是cocos文件夾,就是一樣的,反正找到libs
就行;
為TARGETS為
other linker flags
添加 -Objc把PROJECT和TARGETS的
Enable BitCode
都設(shè)為NO分別修改PROJECT紊撕、TARGETS下
C Language Dialect
為gnu11
注:這里的
gnu11
是我的工程的,根據(jù)自己的cocos的工程查找到對(duì)應(yīng)的,去配置
- 在PROJECT和TARGETS為
Preprocessor Macros
添加預(yù)定義宏 ,具體看工程,所有的宏也根據(jù)自己的cocos的工程查找到對(duì)應(yīng)的
3.7 工程代碼的修改
-
修改Classes文件夾下的
AppDelegate.h
和AppDelegate.cpp
把名稱改成CocosAppDelegate.h
和CocosAppDelegate.cpp
,把代碼內(nèi)的AppDelegate
全部替換成CocosAppDelegate
-
修改Classes文件夾下的
CocosAppDelegate.cpp
和jsb_module_register.cpp
的type
,改成Objective-C++
(這是一個(gè)巨坑,如果你的代碼在不修改的情況下,能正常運(yùn)行,就保持原C++
,如果是報(bào)錯(cuò)就改成Objective-C++
,個(gè)人感覺這個(gè)應(yīng)該和我們后面配置的gnu11 或者 C99有關(guān))
-
把ios文件夾下的
AppController.mm
內(nèi)的#import "AppDelegate.h"
改成#import "CocosAppDelegate.h"
,把下面的app = new CocosAppDelegate(bounds.size.width * scale, bounds.size.height * scale); app->setMultitouch(true);
改掉
很重要的一點(diǎn),前面忘記提了,之前classes文件下的文件改名后,建議是刪除掉,重新導(dǎo)入一次,不然會(huì)出現(xiàn)報(bào)錯(cuò)或者運(yùn)行完界面黑屏 !!!! (PS:Target-->Build Phases-->Compile Sources)
修改 目錄: cocos2d_libs—>platform——>iOS下的 CCApplication-ios.mm 文件的end方法入下(322行)
void Application::end()
{
// delete this;
//
// exit(0);
// 上面是原來的樣子
#if USE_AUDIO
AudioEngine::end();
#endif
// stop main loop
[(MainLoop*)_delegate stopMainLoop];
}
修改它的目的是為了cocos引擎在啟動(dòng)后,退出游戲不自動(dòng)關(guān)閉引擎,不殺死APP,再次進(jìn)入游戲,可以直接開始游戲了,更加的快速;
3.8 搭建游戲引擎交互界面,游戲界面VC
搭建 CocosMng
,這里面包括了所有的JS調(diào)用OC的方法,OC調(diào)用JS的方法,有參數(shù),無參數(shù),多個(gè)參數(shù)等的方法調(diào)用,這個(gè)CocosMng
類名需要提供給cocos開發(fā)人員,里面注釋的方法不是不能用,而是要看cocos給的方法的參數(shù)是如何的,是讓我們傳一個(gè)int,一個(gè)string,還是多個(gè)字符串還是json字符串:
//
// CocosMng.h
//
// cocos引擎管理器黑滴,負(fù)責(zé)iOS啟動(dòng)憨募、退出以及管理cocos引擎
/*
類名不要改變,對(duì)應(yīng)cocos的JS調(diào)用OC的類名;
所有的供JS調(diào)用的方法,使用類方法;
所有的OC調(diào)用的JS方法,統(tǒng)一寫在CocosMng下;
*/
#ifndef CocosMng_h
#define CocosMng_h
#import <UIKit/UIKit.h>
#include "platform/ios/CCEAGLView-ios.h"
@protocol CocosMngDelegate <NSObject>
//游戲開始
-(void)cocosGameStart;
//返回游戲分?jǐn)?shù)
-(void)cocosgameEnd:(int)score;
//退出游戲
-(void)cocosGameClose;
@end
@interface CocosMng : NSObject
{
}
@property(nonatomic, retain) UIViewController* viewController;
@property (nonatomic, weak) id <CocosMngDelegate> delegate;
+(instancetype)getInstance;
+(void) destroyInstance;
-(instancetype)init;
-(void)startCocos; // 啟動(dòng)cocos游戲引擎
-(void)exitCocos; // 退出cocos游戲引擎
+(void)exitCocosForJS; // 給js調(diào)用,js只能調(diào)用靜態(tài)方法
+(CCEAGLView *)getCocosView;
-(void)didEnterBackground; // app進(jìn)入后臺(tái)時(shí)調(diào)用袁辈,通知游戲js層
-(void)willEnterForeground; // app返回前臺(tái)時(shí)調(diào)用, 通知游戲js層
-(void)willTerminate;
//oc 調(diào)用 js 測(cè)試
-(void)testOCForJS;
-(void)gamePauses;//游戲暫停
-(void)gameContinue;//游戲繼續(xù)
-(void)gameOver;//游戲結(jié)束
#pragma mark 初始化參數(shù)設(shè)置
-(void)gameInitDataSet:(NSDictionary *)data;
#pragma mark fish 給JS調(diào)用的方法
//初始化配置,此方法被調(diào)用后,需要調(diào)用recvDataFromNativeWithData把初始化參數(shù)傳遞給JS
+(void)initGameVC;
//游戲開始
+(void)game_GetStar_Start;
//返回游戲分?jǐn)?shù)
+(void)game_GetStar_end:(int)score;
//退出游戲
+(void)closeGameVC;
#pragma mark fish OC調(diào)用JS的方法
//log輸出:printlog
-(void)printlog:(NSString *)logString;
//初始化方法recvDataFromNative(historyMaxFraction, model, max, min)
-(void)recvDataFromNativeWithData:(NSDictionary *)dataDic;
//接收肌電值數(shù)據(jù)onRecvFly(s)
-(void)onRecvFly:(int)num;
//導(dǎo)聯(lián)脫落onDaoLianRecv
-(void)onDaoLianRecv:(BOOL)recv;
//電極脫落onDianJiRecv
-(void)onDianJiRecv:(BOOL)recv;
@end
#endif /* CocosMng_h */
#import "CocosMng.h"
#import "cocos2d.h"
#import "CocosAppDelegate.h"
#import "SDKWrapper.h"
#import "platform/CCApplication.h"
#import "platform/ios/CCEAGLView-ios.h"
#import "cocos/scripting/js-bindings/jswrapper/SeApi.h"
#import "CRBeeGameController_new.h"
using namespace cocos2d;
static CocosMng * _instance = NULL;
static Application * _app = NULL;
@interface CocosMng ()
@property (nonatomic, strong) NSDictionary *gameData;
@end
@implementation CocosMng
+(instancetype)getInstance {
// static dispatch_once_t onceToken;
// dispatch_once(&onceToken, ^{
// _instance = [[self alloc] init];
// });
if (!_instance) {
_instance = [[CocosMng alloc]init];
}
return _instance;
}
+(void) destroyInstance {
if (_instance != NULL) {
_instance = NULL;
}
}
+(CCEAGLView*)getCocosView {
return (__bridge CCEAGLView *)cocos2d::Application::getInstance()->getView();
}
-(instancetype)init {
self = [super init];
if (self) {
float scale = [[UIScreen mainScreen] scale];
CGRect bounds = [[UIScreen mainScreen] bounds];
// cocos2d application instance
if (_app == NULL) {
_app = new CocosAppDelegate(bounds.size.width * scale, bounds.size.height * scale);
_app->setMultitouch(false);
}
// [self setupCocos];
}
return self;
}
//-(void)setupCocos {
// self.viewController = [[CRBeeGameController_new alloc]init];
// [self.viewController setModalPresentationStyle:UIModalPresentationFullScreen];
//}
-(void)startCocos {
NSLog(@"start cocos.");
_app->restart();
return;
}
+ (void)exitCocosForJS {
[[CocosMng getInstance]exitCocos];
}
- (void)exitCocos {
if (_app == NULL) {
return;
}
_app->end(); // mark: 這里修改了CCApplication-ios.mm菜谣,end不再直接退出游戲而是只停止主循環(huán)和音頻播放
[self.viewController dismissViewControllerAnimated:YES completion:NULL];
}
-(void)didEnterBackground {
if (_app != NULL) {
_app->onPause();
}
}
-(void)willEnterForeground {
if (_app != NULL) {
_app->onResume();
}
}
-(void)willTerminate {
[self.viewController dismissViewControllerAnimated:YES completion:NULL];
[CocosMng destroyInstance];
}
//oc 調(diào)用 js
-(void)testOCForJS{
// NSLog(@"%s",__func__);
// std::string strRet = "hahaha";
// std::string jsCallStr = cocos2d::StringUtils::format("onGameStop();"); //Main.js
// se::Value *ret = new se::Value();
// se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str() , -1 , ret);
// NSLog(@"jsCallStr rtn = %s", ret->toString().c_str());
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//測(cè)試 -->OC調(diào)用 JS 的方法 'window.testMethod'
NSLog(@"OC調(diào)用 JS 的方法");
std::string strRet = "hahaha";
std::string jsCallStr = cocos2d::StringUtils::format("window.onGameStop();"); //gamectrl
se::Value *ret = new se::Value();
se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str() , -1 , ret);
NSLog(@"jsCallStr rtn");
// });
}
/*
1.std::string轉(zhuǎn)NSString
std::string _string("hello");
NSString *str= [NSString stringWithCString:_string.c_str() encoding:[NSString defaultCStringEncoding]];
2.NSString轉(zhuǎn)std::string
NSString * nsfaceName=@"HELLO";
const char * lpfaceName = [nsfaceName UTF8String];
std::string strFaceName= [nsfaceName UTF8String];
*/
#pragma mark 游戲暫停
-(void)gamePauses{
std::string strRet = "hahaha";
std::string jsCallStr = cocos2d::StringUtils::format("window.onGameStop();"); //gamectrl
se::Value *ret = new se::Value();
se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str() , -1 , ret);
}
#pragma mark 游戲繼續(xù)
-(void)gameContinue{
std::string strRet = "hahaha";
std::string jsCallStr = cocos2d::StringUtils::format("window.onGameResume();"); //gamectrl
se::Value *ret = new se::Value();
se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str() , -1 , ret);
}
#pragma mark 游戲結(jié)束
-(void)gameOver{
std::string strRet = "hahaha";
std::string jsCallStr = cocos2d::StringUtils::format("window.onGameOver();"); //gamectrl
se::Value *ret = new se::Value();
se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str() , -1 , ret);
}
#pragma mark fish 給JS調(diào)用的方法
//初始化配置,此方法被調(diào)用后,需要調(diào)用recvDataFromNativeWithData把初始化參數(shù)傳遞給JS
+(void)initGameVC{
NSLog(@"游戲初始化中~~~~~~~");
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_instance recvDataFromNativeWithData:_instance.gameData];
// });
}
//游戲開始
+(void)game_GetStar_Start{
NSLog(@"游戲開始~~~~~~~");
if (_instance.delegate && [_instance.delegate respondsToSelector:@selector(cocosGameStart)]) {
[_instance.delegate cocosGameStart];
}
}
//返回游戲分?jǐn)?shù)
+(void)game_GetStar_end:(int)score{
NSLog(@"游戲分?jǐn)?shù)~~~~~~~%d",score);
if (_instance.delegate && [_instance.delegate respondsToSelector:@selector(cocosgameEnd:)]) {
[_instance.delegate cocosgameEnd:score];
}
}
//退出游戲
+(void)closeGameVC{
NSLog(@"游戲退出,關(guān)閉界面~~~~~~~");
if (_instance.delegate && [_instance.delegate respondsToSelector:@selector(cocosGameClose)]) {
[_instance.delegate cocosGameClose];
}
}
#pragma mark 初始化參數(shù)設(shè)置
-(void)gameInitDataSet:(NSDictionary *)data{
if (data) {
_instance.gameData = [data copy];
}else{
//未賦值的情況下,給默認(rèn)初值
_instance.gameData = @{
@"historyMaxFraction":@(200),
@"model":@(1),
@"max":@(40),
@"min":@(10)
};
}
}
#pragma mark fish OC調(diào)用JS的方法
//log輸出:printlog
-(void)printlog:(NSString *)logString{
std::string strRet = [logString UTF8String];//"haha";
std::string jsCallStr = cocos2d::StringUtils::format("window.printlog(\"%s\");", strRet.c_str());
se::Value *ret = new se::Value();
se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str() , -1 , ret);
}
//初始化方法recvDataFromNative(historyMaxFraction, model, max, min)
-(void)recvDataFromNativeWithData:(NSDictionary *)dataDic{
NSString *hisScore = [NSString stringWithFormat:@"%@",dataDic[@"historyMaxFraction"]];
NSString *model = [NSString stringWithFormat:@"%@",dataDic[@"model"]];
NSString *max = [NSString stringWithFormat:@"%@",dataDic[@"max"]];
NSString *min = [NSString stringWithFormat:@"%@",dataDic[@"min"]];
std::string hisScoreRet = [hisScore UTF8String];
std::string modelScoreRet = [model UTF8String];
std::string maxScoreRet = [max UTF8String];
std::string minScoreRet = [min UTF8String];
std::string jsCallStrNew = cocos2d::StringUtils::format("window.recvDataFromNative2(\"%s\",\"%s\",\"%s\",\"%s\");", hisScoreRet.c_str(),modelScoreRet.c_str(),maxScoreRet.c_str(),minScoreRet.c_str());
se::ScriptEngine::getInstance()->evalString(jsCallStrNew.c_str());
// int historyMaxFraction = [dataDic[@"historyMaxFraction"] intValue];
// int model = [dataDic[@"model"] intValue];
// int max = [dataDic[@"max"] intValue];
// int min = [dataDic[@"min"] intValue];
// NSLog(@"初始參數(shù):%d,%d,%d,%d",historyMaxFraction,model,max,min);
//
//// std::string jsCallStr = cocos2d::StringUtils::format("window.recvDataFromNative(\"%d,%d,%d,%d\");", historyMaxFraction,model,max,min);
// std::string jsCallStr = cocos2d::StringUtils::format("window.recvDataFromNative(\"%d\",\"%d\",\"%d\",\"%d\");", historyMaxFraction,model,max,min);
//
// se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str());
}
//接收肌電值數(shù)據(jù)onRecvFly(s)
-(void)onRecvFly:(int)num{
std::string jsCallStr = cocos2d::StringUtils::format("window.onRecvFly(\"%d\");", num);
se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str());
}
//導(dǎo)聯(lián)脫落onDaoLianRecv
-(void)onDaoLianRecv:(BOOL)recv{
std::string jsCallStr = cocos2d::StringUtils::format("window.onDaoLianRecv(\"%d\");", recv == 0?false:true);
se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str());
}
//電極脫落onDianJiRecv
-(void)onDianJiRecv:(BOOL)recv{
std::string jsCallStr = cocos2d::StringUtils::format("window.onDianJiRecv(\"%d\");", recv == 0?false:true);
se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str());
}
//
//- (void)statusBarOrientationChanged:(NSNotification *)notification {
// if (_app == NULL) {
// return;
// }
//
// CGRect bounds = [UIScreen mainScreen].bounds;
// float scale = [[UIScreen mainScreen] scale];
// float width = bounds.size.width * scale;
// float height = bounds.size.height * scale;
// Application::getInstance()->updateViewSize(width, height);
//}
//
//- (void)applicationWillResignActive:(UIApplication *)application {
// /*
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
// */
// if (_app == NULL) {
// return;
// }
//
// _app->onPause();
// [[SDKWrapper getInstance] applicationWillResignActive:application];
//}
//
//- (void)applicationDidBecomeActive:(UIApplication *)application {
// /*
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// */
// if (!_app) {
// return;
// }
//
// _app->onResume();
// [[SDKWrapper getInstance] applicationDidBecomeActive:application];
//}
//
//- (void)applicationDidEnterBackground:(UIApplication *)application {
// /*
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
// */
// if (!_app) {
// return;
// }
//
// [[SDKWrapper getInstance] applicationDidEnterBackground:application];
//}
//
//- (void)applicationWillEnterForeground:(UIApplication *)application {
// /*
// Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
// */
// if (!_app) {
// return;
// }
//
// [[SDKWrapper getInstance] applicationWillEnterForeground:application];
//}
//
//- (void)applicationWillTerminate:(UIApplication *)application
//{
// if (!_app) {
// return;
// }
//
// [[SDKWrapper getInstance] applicationWillTerminate:application];
//// delete _app;
//// _app = nil;
//}
//
//#pragma mark -
//#pragma mark Memory management
//
//- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
// /*
// Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
// */
// NSLog(@"%s",__func__);
//}
//
@end
搭建游戲VC:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface GameViewController : UIViewController
{
}
- (BOOL)prefersStatusBarHidden;
@end
NS_ASSUME_NONNULL_END
#import "GameViewController.h"
#import "CocosMng.h"
#import "AppDelegate.h"
#import "platform/ios/CCEAGLView-ios.h"
#import "SDKWrapper.h"
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
#import "cocos2d.h"
//#import "AppDelegate.h"
@interface GameViewController ()
@end
@implementation GameViewController
//- (void) loadView {
// UIView *cocosViw = [CocosMng getCocosView];
// if (cocosViw != nil) {
// self.view = [CocosMng getCocosView];
// } else {
// NSLog(@"初始化cocos view失敗!");
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// [self dismissViewControllerAnimated:YES completion:NULL];
// });
// }
//}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
NSLog(@"---%s", __func__);
// Do any additional setup after loading the view.
// 支持多個(gè)view
// self.view.backgroundColor = [[UIColor alloc] initWithRed:160 / 255.0 green:140 / 255.0 blue:255 / 255.0 alpha:1];
// self.view.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"上課背景圖"].CGImage);
// // ((GLKView *)self.view).delegate = self;
// if ([[UIDevice currentDevice].model isEqualToString:@"iPhone"]) {
// self.view.layer.contents = nil;
// self.view.backgroundColor = [UIColor blackColor];
// }
// UIView *videoView = [[UIView alloc] initWithFrame:CGRectMake(50, 70, 150, 150)];
// [self.view addSubview:videoView];
// videoView.tag = kPTGameVideoPlayerViewTag;
UIView *_cocosView = [CocosMng getCocosView];
if (_cocosView == NULL) {
NSLog(@"創(chuàng)建cocos view失敗!");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:true completion:NULL];
});
return;
}
_cocosView.frame = self.view.bounds;
_cocosView.backgroundColor = UIColor.clearColor;
[self.view addSubview:_cocosView];
// UIView *courseVideoView = [[UIView alloc] initWithFrame:CGRectZero];
// [self.view addSubview:courseVideoView];
// courseVideoView.tag = kPTGameCourseVideoPlayerViewTag;
//
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// [PTNativeFunc showNoRecordToast:@"為保證您的上課體驗(yàn)尾膊,建議您使用更高性能的設(shè)備上課媳危!"];
// });
// [PTNativeFunc onNetBlock:^{
// [PTCocos2D onNoNetShowDialog];
// }];
[[UIApplication sharedApplication] setStatusBarHidden:true];
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// NSLog(@"游戲暫停-------->");
// [[CocosMng getInstance] gamePauses];
// });
//
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// NSLog(@"游戲繼續(xù)-------->");
// [[CocosMng getInstance] gameContinue];
// });
//
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(30 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// NSLog(@"游戲結(jié)束-------->");
// [[CocosMng getInstance] gameOver];
// });
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
- (BOOL)prefersHomeIndicatorAutoHidden {
return YES;
}
#pragma mark - 橫屏設(shè)置
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self checkLandscapeRight];
}
- (void)checkLandscapeRight {
[self switchNewOrientation:UIInterfaceOrientationLandscapeRight];
}
void switchNewOrientation(UIInterfaceOrientation interfaceOrientation)
{
NSNumber *orientationTarget = [NSNumber numberWithInt:interfaceOrientation];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
- (void)switchNewOrientation:(UIInterfaceOrientation)orientation
{
// AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
// //允許轉(zhuǎn)成橫屏
// appDelegate.allowRotation = orientation != UIInterfaceOrientationPortrait;
//調(diào)用橫屏代碼
switchNewOrientation(orientation);
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSLog(@"---%s", __func__);
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(@"---%s", __func__);
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
NSLog(@"---%s", __func__);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
NSLog(@"---%s", __func__);
}
- (void)becomeActive {
NSLog(@"---%s", __func__);
}
- (void)willResginActive {
NSLog(@"---%s", __func__);
}
- (void)willEnterForeground {
NSLog(@"---%s", __func__);
}
- (void)didEnterBackground {
NSLog(@"---%s", __func__);
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
進(jìn)入游戲VC的界面VC
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
#import "CocosMng.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"ViewController");
self.view.backgroundColor = [UIColor whiteColor];
// Do any additional setup after loading the view.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self presentViewController:[CocosMng getInstance].viewController animated:YES completion:nil];
NSLog(@"啟動(dòng)cocos引擎");
[[CocosMng getInstance] startCocos];
});
}
- (IBAction)startCocos:(id)sender {
GameViewController * vc = [[GameViewController alloc] init];
[CocosMng getInstance].viewController = vc;
[self presentViewController:[CocosMng getInstance].viewController animated:YES completion:nil];
NSLog(@"啟動(dòng)cocos引擎");
[[CocosMng getInstance] startCocos];
}
@end
到此,整個(gè)配置流程完成
4 報(bào)錯(cuò)
很多報(bào)錯(cuò)沒有總結(jié)進(jìn)來忘記了,這里就幾個(gè)
- 出現(xiàn)setting.js 找不到—>直接clear后,卸載APP重新安裝,緩存問題導(dǎo)致的;(有可能是)
-
在導(dǎo)入Class后報(bào)錯(cuò):Unknown type name 'NSString'
解決辦法就是把Class下的兩個(gè)cpp文件修改type(上面提到了)
- Multiple commands produce LaunchScreen.storyboardc
我的做法是刪除Target--->Build Phases
下的info文件,同時(shí)進(jìn)行如下設(shè)置:
如果不修改Projetc Setting(有??),也可以刪除掉自己工程中的Main 和 LaunchScreen(如果沒有用到的話)
- 在cocos 2.4.5的版本上,生成的iOS工程,集成時(shí),出現(xiàn)
Undefined symbols for architecture x86_64
報(bào)錯(cuò),如下:
我發(fā)現(xiàn),采用上面的這些配置正確配置,還是有這個(gè)問題,不知道是新的版本需要添加什么配置,有找到的大佬請(qǐng)留言解惑; 我的解決辦法是把cocos2.4.3生成的cocos2d-x
文件夾,替換掉新的cocos2d-x
文件夾,這樣可以解決,但是不知道原因;
后期還有集成了游戲后的上架問題,也是需要處理的,這里還沒上架,后期上架再補(bǔ)上了.