2013-04-17 28 views
1

我使用一个按钮创建了常规简单iOS应用程序(单一视图应用程序),并且我想从iOS应用程序启动一个cocos2D游戏。换句话说,当我按myapp.xib中的按钮时,我想启动cocos2D游戏。我不想使用URL方案来启动游戏,因为这需要用户下载我的应用以及下载游戏。我希望用户能够从我的应用程序内部启动游戏。这是这场比赛我想从我的应用程序启动:如何从单一视图应用程序启动cocos2D游戏

http://www.raywenderlich.com/14439/how-to-make-a-game-like-fruit-ninja-with-box2d-and-cocos2d-part-3

我能够成功加入这个游戏,因为在我的Xcode项目的依赖。但是,我不知道如何去从应用程序启动游戏。

以下是我的一些想法,但它们并不适合我。 有什么办法,我可以:

  • 呼叫从IBAction为游戏(不是我的应用程序的应用程序委托)的在我的应用程序委托,以启动游戏?
  • 从我的应用中的IBAction调用游戏的didFinishLaunchingWithOptions方法(而不是我的应用的didFinishLaunchingWithOptions)来启动游戏?
  • 从我的应用程序中的IBAction调用游戏的main.m文件(不是我的应用程序的main.m文件)来启动游戏?

据我所知,这些是启动iOS应用程序的三种不同方式。请记住,我正在开发一个应用程序(而不是游戏),它将允许用户通过应用程序在内部启动上述游戏。理想情况下,如果我可以简单地从我的应用程序执行pushviewcontroller到游戏,它会很好(也很容易),但我不确定是否有一种简单的方法来处理这个问题。

无论如何,我可以通过我的应用程序内部启动这个游戏吗?任何意见,建议,示例源代码将不胜感激。

回答

1

简短的回答:

您将需要只有一个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]; 

希望它有帮助。

+0

嘿ssantos,你可以指定你的意思是“cocos2d init”的东西。代码是: - 游戏的应用程序委托? - “YourFirstGameScene”.mm? – Kofi

+0

我的意思是游戏代表的初始化代码,因为场景inits应该保持在原来的位置。我以示例更新我的答案 – ssantos

+0

到目前为止,我试图链接库。所以这是有效的。但是,每当我按下按钮时,我都会崩溃。这是我得到的错误。我必须将YourFirstGameScene作为@class来使应用程序运行。 **断言失败 - [CCDirectorDisplayLink pushScene:] ***终止应用程序,由于未捕获的异常'NSInternalInconsistencyException',原因:'参数必须是非零 – Kofi