ioscocos2d-iphoneeaglview

Attach CCScene to EAGLView


I am trying to attach my CCScene class to my EAGLView. So my EAGLView is an IBOutlet in Class 1. I connected it and settered and gettered it. Then in my ViewDidLoad in my Class 1 I do:

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

The problem is it is not calling the class that I show below in my CCScene class (I tested with NSLogs). Anyway lets say my CCScene class is named CCSceneClass, how would I properly connect it from my ViewDidLoad from Class1 (My UIViewController) so that this method in my CCScene class gets called?

Also this is some code that I do in this method if this matters at all:

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

Thanks!

My Issue:

1. What is the point of these 2 lines (btw the first line came up with a warning saying that, that method was not found:

[director setDirectorType:kCCDirectorTypeDisplayLink];
[director setAnimationInterval:1.0/60];

I already have a game loop in my CCLayer class so does that mean I can just get rid of these lines?

That is the only question I have left!

Thanks!


Solution

  • You can not "connect" a CCScene class with the OpenGL view, and there's no need to really.

    After you've set the eaglView you would proceed as normal to run your scene with CCDirector:

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

    If you need access to the eaglView inside your scene class, you get this via CCDirector:

    EAGLView* view = [CCDirector sharedDirector].openglView;
    

    UPDATE:

    Full code to initialize Cocos2D view in a UIKit app:

    CCDirector* director = [CCDirector sharedDirector];
    [director setDirectorType:kCCDirectorTypeDisplayLink];
    [director setAnimationInterval:1.0/60];
    [director setOpenGLView:(EAGLView*)subview];
    [director runWithScene:[HelloWorldLayer scene]];
    

    There's a chapter in my Learn Cocos2D book explaining integration of Cocos2D in a UIKit app (and vice versa) in more detail. You can also download the book's source code (2nd edition) from that page and look at the Chapter 15 example project "ViewBasedAppWithCocos2D".