开发者

cocos2d scene in UIViewController

I am trying to show a cocos2D-iPhone scene from within a UIVie开发者_开发知识库wController. Cocos2d should use the UIViewController's view as the rendering area. How could this be done?

(Targeting iOS 4.2)

Thanks in advance!


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!


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜