1
votes

I am trying to show a cocos2D-iPhone scene from within a UIViewController. Cocos2d should use the UIViewController's view as the rendering area. How could this be done?

(Targeting iOS 4.2)

Thanks in advance!

2

2 Answers

2
votes

I think what you are looking for is in the tests in the cocos2d source file.

Look at 'attachDemo' under the 'tests' folder.

It demonstrates adding and removing a cocos2d view.

It seems to create an EAGLView and add that as a subview of the UIView, then set up the director and cocos2d as normal within that glview.

mainView is a UIView.

    EAGLView *glview = [EAGLView viewWithFrame:CGRectMake(0, 0, 250,350)];
    [mainView addSubview:glview];

    CCDirector *director = [CCDirector sharedDirector];
    [director setOpenGLView:glview];

    CCScene *scene = [CCScene node];
    id node = [LayerExample node];
    [scene addChild: node];

    [director runWithScene:scene];

Hope this solves your problem!

0
votes

For those who are using Cocos2D 2.x and XCode 4.3 with storyboard, I recommend this blog post: http://www.tinytimgames.com/2012/02/07/cocos2d-and-storyboards/

Because some functions are deprecated since Cocos2D 1.x.

Totally worked :)