【iOS】Cocos2dx封裝為view方便做3D動畫效果

來源:互聯網
上載者:User

著作權聲明:本文為博主原創,如需轉載請註明出處。 寫在前面

本檔案只是將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()
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.