著作權聲明:本文為博主原創,如需轉載請註明出處。 寫在前面
本檔案只是將Cocos2dx 封裝為一個 UIView,方便直接添加使用
使用的 coco2d-x 版本為 3.13.1 添加過程
首先將 ~/cocos2d/build/cocos2d_lib.xcodeproj 添加到工程中
然後對照new出來的工程,修改配置:
然後添加兩個 Header Search Paths,同樣對照樣本工程
設定TARGETS中Header Search Paths
PROJECT中設定Header Search Paths,相對路徑
設定預先處理的宏定義,Debug和Release
修改Custom Compiler Flags
至此,不出意外編譯可以成功了。 設定view
然後在CocosViewController(我自己建立的測試介面)中設定彈出HelloWorld介面,HelloWorld介面的兩個類 AppDelegate 和 HelloWorldScene要添加到工程中。
注意這個檔案是C++混編,所以 CocosViewController.m 檔案要改尾碼為 CocosViewController.mm
#import "CocosViewController.h"#import "CocosAppDelegate.h"#import "platform/ios/CCEAGLView-ios.h"#import "platform/ios/CCDirectorCaller-ios.h"// cocos2d application instancestatic CocosAppDelegate s_sharedApplication;@interface CocosViewController ()@property (weak, nonatomic) UIView *blackView;@property (weak, nonatomic) CCEAGLView *eaglView;@end@implementation CocosViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; [self drawButton];}#pragma mark - 繪製按鈕- (void)drawButton{ UIButton *cocoaButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 70, 80, 50)]; [cocoaButton setTitle:@"動畫開關" forState:UIControlStateNormal]; [cocoaButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [cocoaButton addTarget:self action:@selector(presentCocosView) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:cocoaButton];}- (void)presentView{ if (self.blackView) { [self.blackView removeFromSuperview]; self.blackView = nil; } else { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, 120, [UIScreen mainScreen].bounds.size.width / 2.0, [UIScreen mainScreen].bounds.size.height / 2.0)]; [view setBackgroundColor:[UIColor blackColor]]; [self.view addSubview:view]; self.blackView = view; }}- (void)presentCocosView{ if (self.eaglView) { [self destroyCocosView]; } else { [self addCocosView]; }}- (void)addCocosView{ cocos2d::Application *app = cocos2d::Application::getInstance(); // Initialize the GLView attributes app->initGLContextAttrs(); cocos2d::GLViewImpl::convertAttrs(); // Initialize the CCEAGLView CCEAGLView *eaglView = [CCEAGLView viewWithFrame: CGRectMake(0, 120, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height * 0.8) pixelFormat: (__bridge NSString *)cocos2d::GLViewImpl::_pixelFormat depthFormat: cocos2d::GLViewImpl::_depthFormat preserveBackbuffer: NO sharegroup: nil multiSampling: NO numberOfSamples: 0 ]; // Enable or disable multiple touches [eaglView setMultipleTouchEnabled:NO]; // Set EAGLView as view of RootViewController // self.view = eaglView; [self.view addSubview:eaglView]; self.eaglView = eaglView; cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView((__bridge void *)eaglView); //set the GLView as OpenGLView of the Director cocos2d::Director::getInstance()->setOpenGLView(glview); //run the cocos2d-x game scene app->run();}- (void)destroyCocosView{ // cocos2d::Director::getInstance()->getTextureCache() cocos2d::PoolManager::destroyInstance(); cocos2d::Director::getInstance()->purgeCachedData(); cocos2d::Director::getInstance()->purgeDirector(); [CCDirectorCaller destroy];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
此時就可以重複開啟關閉cocos動畫介面了。 釋放cocos單例 檢查單例
這裡先對所有的cocos2dx中的單例進行檢查,主要是想用Director銷毀其他的單例,經過檢查的結果也證明了王哲說的:用Director來管理項目中的單例,具體釋放在上面的 destroyCocosView 方法中。即為:
- (void)destroyCocosView{ // cocos2d::Director::getInstance()->getTextureCache() cocos2d::PoolManager::destroyInstance(); cocos2d::Director::getInstance()->purgeCachedData(); cocos2d::Director::getInstance()->purgeDirector(); [CCDirectorCaller destroy];}
下面為檢查結果表格:
單例 |
是否被cocostudio釋放 |
啟動時是否被執行個體化 |
ArmatureNodeReader::destroyInstance() |
是 |
否 |
ButtonReader::destroyInstance() |
是 |
否 |
ActionManagerEx::destroyInstance() |
是 |
CCActionNode 和 CCSGUIReader |
BoneNodeReader::destroyInstance() |
否 |
否 |
ActionTimelineCache::destroyInstance() |
CSLoader銷毀時會調用這個銷毀 |
CSLoader |
Animation3DCache::destroyInstance() |
否 |
否 |
AnimationCache::destroyInstance() |
是 |
CCNodelLoader 和 CCSprite |
ScriptEngineManager::destroyInstance() |
否 |
有一堆類用到這個執行個體,但沒有被銷毀,但可以用Application銷毀 |
PoolManager::destroyInstance() |
否 |
同上 |
ArmatureDataManager::destroyInstance() |
是 |
否 |
AsyncTaskPool::destroyInstance() |
是 |
其他3個類有用到 |
Configuration::destroyInstance() |
是 |
二十幾個類用到 |
FileUtils::destroyInstance() |
是 |
|
GLProgramCache::destroyInstance() |
是 |
|
GLProgramStateCache::destroyInstance() |
是 |
|
NodeLoaderLibrary::destroyInstance() |
否 |
否 |
__NotificationCenter::destroyInstance() |
否 |
否 |
GUIReader::destroyInstance() |
是 |
十幾個類調用 |
Sprite3DCache::getInstance() |
否 |
自己調用 |
Sprite3DMaterialCache::destroyInstance() |
否 |
|
SpriteFrameCache::destroyInstance() |
是 |
|
SceneReader::destroyInstance() |
是 |
|
DictionaryHelper::destroyInstance() |
是 |
|
UserDefault::destroyInstance() |
是 |
|
CheckBoxReader::destroyInstance() |
是 |
|
NodeReader::destroyInstance() |
是 |
|
SingleNodeReader::destroyInstance() |
是 |
|
SpriteReader::destroyInstance() |
是 |
|
ParticleReader::destroyInstance() |
是 |
|
GameMapReader::destroyInstance() |
是 |
|
ProjectNodeReader::destroyInstance() |
是 |
|
ComAudioReader::destroyInstance() |
是 |
|
WidgetReader::destroyInstance() |
是 |
|
CheckBoxReader::destroyInstance() |
是 |
|
ImageViewReader::destroyInstance() |
是 |
|
TextBMFontReader::destroyInstance() |
是 |
|
TextReader::destroyInstance() |
是 |
|
TextFieldReader::destroyInstance() |
是 |
|
TextAtlasReader::destroyInstance() |
是 |
|
LoadingBarReader::destroyInstance() |
是 |
|
SliderReader::destroyInstance() |
是 |
|
LayoutReader::destroyInstance() |
是 |
|
ScrollViewReader::destroyInstance() |
是 |
|
PageViewReader::destroyInstance() |
是 |
|
ListViewReader::destroyInstance() |
是 |
|
Node3DReader::destroyInstance() |
是 |
|
Sprite3DReader::destroyInstance() |
是 |
|
UserCameraReader::destroyInstance() |
是 |
|
Particle3DReader::destroyInstance() |
是 |
|
CSLoader::destroyInstance() |
是 |
|
FlatBuffersSerialize::destroyInstance() |
否 |
一堆調用這個 |
GameNode3DReader::destroyInstance() |
否 |
否 |
HttpClient::destroyInstance() |
否 |
否 |
Light3DReader::destroyInstance() |
否 |
否 |
JsonLocalizationManager::destroyInstance() |
否 |
否 |
BinLocalizationManager::destroyInstance() |
否 |
否 |
ObjectFactory::destroyInstance() |
否 |
是 |
SkeletonNodeReader::destroyInstance() |
否 |
否 |
SocketIO::destroyInstance() |
否 |
否 |
TabControlReader::destroyInstance() |
否 |
否 |
TabHeaderReader::destroyInstance() |
否 |
否 |
TabItemReader::destroyInstance() |
否 |
否 |
TriggerMng::destroyInstance() |
是 |
|