ios - 将 CCScene 附加到 EAGLView

标签 ios cocos2d-iphone eaglview

我正在尝试将我的 CCScene 类(class)附加到我的 EAGLView。所以我的 EAGLView 是 1 类中的 IBOutlet。我连接它并设置和获取它。然后在我的 Class 1 的 ViewDidLoad 中,我这样做:

[[CCDirector sharedDirector] setOpenGLView:self.eaglView];
[[CCDirector sharedDirector] runWithScene:[CCSceneClass node]];

问题是它没有调用我在 CCScene 类中显示的类(我用 NSLogs 测试过)。无论如何,可以说我的 CCScene 类名为 CCSceneClass,我将如何从 Class1(我的 UIViewController)的 ViewDidLoad 正确连接它,以便调用我的 CCScene 类中的这个方法?

如果这很重要,这也是我在此方法中执行的一些代码:
-(id)initWithEaglView:(EAGLView*)view {
NSLog(@"initWithEAGLView");
        //Attach CCDirector to EAGLView
        director = [CCDirector sharedDirector];
        [director setOpenGLView:view];
        //Make CCLayer and CCScene
        CCScene *scene = [CCScene node];
        CCLayer *layer = [CCLayer node];
        [scene addChild:layer];
        [director runWithScene:scene];
        [director setDisplayFPS:NO];
}

谢谢!

我的问题:

1. 这两行有什么意义(顺便说一句,第一行提出了一个警告,即找不到该方法:
[director setDirectorType:kCCDirectorTypeDisplayLink];
[director setAnimationInterval:1.0/60];

我的 CCLayer 类中已经有一个游戏循环,这是否意味着我可以摆脱这些线?

这是我留下的唯一问题!

谢谢!

最佳答案

您不能将 CCScene 类与 OpenGL View “连接”起来,实际上也没有必要。

设置好 eaglView 后,您将照常使用 CCDirector 运行场景:

[[CCDirector sharedDirector] setOpenGLView:self.eaglView];
...
[[CCDirector sharedDirector] runWithScene:[CCSceneClass node]];

如果您需要访问场景类中的 eaglView,您可以通过 CCDirector 获得:
EAGLView* view = [CCDirector sharedDirector].openglView;

更新:

在 UIKit 应用中初始化 Cocos2D View 的完整代码:
CCDirector* director = [CCDirector sharedDirector];
[director setDirectorType:kCCDirectorTypeDisplayLink];
[director setAnimationInterval:1.0/60];
[director setOpenGLView:(EAGLView*)subview];
[director runWithScene:[HelloWorldLayer scene]];

我的Learn Cocos2D book里有一章更详细地解释 Cocos2D 在 UIKit 应用程序中的集成(反之亦然)。您也可以下载book's source code (第 2 版)从该页面查看第 15 章示例项目“ViewBasedAppWithCocos2D”。

关于ios - 将 CCScene 附加到 EAGLView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8147444/

相关文章:

ios - 我不应该在核心数据中存储什么?

iphone - 带有 nib 的 ViewController 的子类

ios - 关于如何在不损失图像质量的情况下缩放全屏 iPad 图像的任何建议?

ios - GLKit GLKVertexAttribColor : Dynamically changing colours

ios - 日期格式返回 null

ios - 如何通过facebook SDK在iOS中获取用户名和密码

xcode - Cocos2d 2.1和Xcode7 iOS9崩溃着色器

ios - 将 Sprite 放置在屏幕底部,与分辨率无关

iphone - OpenGLES 中的屏幕到世界坐标转换是一项简单的任务吗?

iphone - 如何在 UIView 和 EAGLView 之间进行混合?