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开发者_JAVA技巧 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.
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.
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
精彩评论