【问题标题】:How to launch a cocos2D game from single view app如何从单视图应用程序启动 cocos2D 游戏
【发布时间】:2013-04-17 23:06:21
【问题描述】:

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

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 会很好(也很容易),但我不确定是否有一种简单的方法来解决这个问题。

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

【问题讨论】:

    标签: iphone ios objective-c xcode cocos2d-iphone


    【解决方案1】:

    简短回答:

    您只需要一个AppDelegate(您的iOS 应用程序之一),然后将所有cocos2D init 内容移到那里。然后,您可以通过您的IBAction 使用类似这样的方式启动游戏。-

    CCDirector *director = [CCDirector sharedDirector];
    [director pushScene:[YourFirstGameScene node]]; 
    [director resume];
    [self presentModalViewController:director animated:YES];
    

    请以下一个 sn-p 为例,从您的 appDelegate 初始化/结束 cocos2d

    - (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;
    }
    

    实际上,我所做的只是在推送我的director 之前调用initCocos,并在关闭游戏之后调用endCocos,比如

    AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    [[CCDirector sharedDirector] dismissModalViewControllerAnimated:YES];
    [appDelegate endCocos];
    

    希望对你有帮助。

    【讨论】:

    • 嘿,ssantos,你能具体说明“cocos2d init”的意思吗?那是代码:-游戏的应用程序委托吗? -“YourFirstGameScene”.mm?
    • 我的意思是游戏委托上的初始化代码,因为场景初始化应该保持在原来的位置。我正在用一个例子更新我的答案
    • 到目前为止,我尝试链接库。所以这行得通。但是,每当我按下按钮时,我都会崩溃。这是我得到的错误。为了让应用程序运行,我必须将 YourFirstGameScene 设为 @class。 ** -[CCDirectorDisplayLink pushScene:]*** 中的断言失败,由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“参数必须为非零
    • 好吧,您似乎将nil 传递给pushScene。你能追踪你传递的值吗?
    猜你喜欢
    • 2018-10-14
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多