简短的回答:是
您将需要只有一个AppDelegate
(iOS应用程式的一个),并搬到那里所有的cocos2D
初始化的东西。然后,您可以用的东西从你的IBAction
启动游戏像this.-
CCDirector *director = [CCDirector sharedDirector];
[director pushScene:[YourFirstGameScene node]];
[director resume];
[self presentModalViewController:director animated:YES];
请采取下一个片段作为一个例子,从您的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;
}
其实初始化/结束cocos2d
,我做的是initCocos
打电话只是刚刚闭幕的比赛之后我推director
,并且endCocos
之前,像
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[[CCDirector sharedDirector] dismissModalViewControllerAnimated:YES];
[appDelegate endCocos];
希望它有帮助。
嘿ssantos,你可以指定你的意思是“cocos2d init”的东西。代码是: - 游戏的应用程序委托? - “YourFirstGameScene”.mm? – Kofi
我的意思是游戏代表的初始化代码,因为场景inits应该保持在原来的位置。我以示例更新我的答案 – ssantos
到目前为止,我试图链接库。所以这是有效的。但是,每当我按下按钮时,我都会崩溃。这是我得到的错误。我必须将YourFirstGameScene作为@class来使应用程序运行。 **断言失败 - [CCDirectorDisplayLink pushScene:] ***终止应用程序,由于未捕获的异常'NSInternalInconsistencyException',原因:'参数必须是非零 – Kofi