简短的回答:是的
您只需要一个AppDelegate
(您的 iOS 应用程序之一),然后将所有cocos2D
初始化内容移到那里。然后,您可以IBAction
使用类似这样的方式从您的游戏中启动游戏。-
CCDirector *director = [CCDirector sharedDirector];
[director pushScene:[YourFirstGameScene node]];
[director resume];
[self presentModalViewController:director animated:YES];
请以下一个片段为例cocos2d
从您的初始化/结束appDelegate
- (void) initCocos {
// Create an CCGLView with a RGB565 color buffer, and a depth buffer of 0-bits
CCGLView *glView = [CCGLView viewWithFrame:[self.window bounds]
pixelFormat:kEAGLColorFormatRGBA8 //kEAGLColorFormatRGBA8
depthFormat:0 //GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
self.director = (CCDirectorIOS*) [CCDirector sharedDirector];
self.director.wantsFullScreenLayout = YES;
// Display FSP and SPF
[self.director setDisplayStats:NO];
// set FPS at 60
[self.director setAnimationInterval:1.0/60];
// attach the openglView to the director
[self.director setView:glView];
// for rotation and other messages
[self.director setDelegate:self];
// 2D projection
[self.director setProjection:kCCDirectorProjection2D];
// [director setProjection:kCCDirectorProjection3D];
// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [self.director enableRetinaDisplay:NO] )
CCLOG(@"Retina Display Not supported");
// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
// You can change anytime.
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
// When in iPhone RetinaDisplay, iPad, iPad RetinaDisplay mode, CCFileUtils will append the "-hd", "-ipad", "-ipadhd" to all loaded files
// If the -hd, -ipad, -ipadhd files are not found, it will load the non-suffixed version
//[CCFileUtils setiPhoneRetinaDisplaySuffix:@"-hd"]; // Default on iPhone RetinaDisplay is "-hd"
[CCFileUtils setiPadSuffix:@""]; // Default on iPad is "" (empty string)
//[CCFileUtils setiPadRetinaDisplaySuffix:@"-ipadhd"]; // Default on iPad RetinaDisplay is "-ipadhd"
// Assume that PVR images have premultiplied alpha
[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];
}
- (void) endCocos {
CC_DIRECTOR_END();
self.director = nil;
}
实际上,我所做的initCocos
只是在推送我的之前director
和endCocos
关闭游戏之后调用,比如
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[[CCDirector sharedDirector] dismissModalViewControllerAnimated:YES];
[appDelegate endCocos];
希望能帮助到你。