Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating Cocos2D with UIKit

I've looked around, and i've seen very little material on how to integrate cocos2d with UIKit (note: not the other way around). What i mean is... for example... adding a cocos sprite animation inside a UIView, which is placed inside a split-view controller (as a subview). How can i do that!?

I want to start with a UISplitView project template or the UITabBar project template.

p.s. I've been doing iPhone development for a while now, but i'm a noob when it comes to cocos2d framework.

like image 464
Mustafa Avatar asked Apr 06 '11 12:04

Mustafa


2 Answers

There's a demo in Cocos2d called AttachDemo, where it attaches a Cocos2d director to a UIView. If you check the method called -(void)runCocos2d.

If you look at its code, it does the following:

-(void) runCocos2d
{
    if( state == kStateEnd ) {

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

        state = kStateRun;
    }
    else {
        NSLog(@"End the view before running it");
    }
}

As you can see, you need to create a EAGLView, attach a director to it, and then simply add that view to the view hierarchy.

like image 136
pgb Avatar answered Sep 29 '22 14:09

pgb


Here is the link of the demo @pgb is referring to, http://code.google.com/p/cocos2d-iphone/source/browse/trunk/tests/attachDemo/attachDemo.m?r=1682

like image 30
cndv Avatar answered Sep 29 '22 14:09

cndv