Using Cocos2D in a UIKit project [Ben Williams] (iDevBlogADay)

I recently needed to add a “water ripple” effect to a project. Although there is a simple water ripple effect built-in to the SDK, it really didn’t fill my needs. It seemed like my only options would be to quickly boost my OpenGL skills (from none to something), or find an alternative. I soon found a really nice water ripple effect built for Cocos2D, which was exactly what I wanted. The only stumbling block was how to integrate Cocos2D into my existing UIKit project. As it turns out, it’s fairly simple.

First, download the source for Cocos2D. If your UIKit project is using ARC, you’ll need to download the “bleeding edge” version from GitHub, as this is currently the only version in which the header files are all ARC friendly. If you are not using ARC, the latest stable version should be fine (currently v1.0.1). Once you have the Cocos2D source, locate the file “cocos2d-ios.xcodeproj”. Open up your UIKit project, and drag this file into your file navigator. You should end up with something like this:

Next, go to the Build Phases settings for your project, and add the Cocos2D library (libcocos2d.a) as a linked library. You’ll probably also need to add OpenGLES.framework, and libz.dylib, like so:

You’ll also need to modify your Build Settings. Set Always Search User Paths to YES, and add the Cocos2D source directory to the User Header Search Paths (as a recursive path):

At this point, you should be able to import the “cocos2d.h” header into one of your source files, and compile your project. If you are using ARC and get some build errors, make sure your Cocos2D source is the development (bleeding edge) branch. Even though Cocos2D itself is not ARC enabled, it’s no problem to combine it with an ARC enabled project, provided you are using the correct version.

Now that Cocos2D is part of your project, it’s time to write some code. I only needed Cocos2D in one small part of my application, so I decided to limit it to a single UIViewController. This is the code that I used:

 - (void)viewDidLoad { 	[super viewDidLoad];  	CCScene *introScene = [CCScene node]; 	self.introLayer = [IntroLayer node]; 	[introScene addChild:self.introLayer];  	if(![CCDirector setDirectorType:kCCDirectorTypeDisplayLink]) { 		[CCDirector setDirectorType:kCCDirectorTypeDefault]; 	}  	CCDirector *director = [CCDirector sharedDirector]; 	EAGLView *glView = [EAGLView viewWithFrame:CGRectMake(0, 0, 1024, 768) 			pixelFormat:kEAGLColorFormatRGB565 depthFormat:0]; 	[director setOpenGLView:glView]; 	[director setDeviceOrientation:kCCDeviceOrientationPortrait]; 	[director setAnimationInterval:1.0/60]; 	[self setView:glView]; 	[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGB565]; 	[[CCDirector sharedDirector] runWithScene:introScene];  	[self.introLayer setupScene]; }  - (void)runRippleAnimation { 	[self.introLayer runRippleAnimation]; }  - (void)endRippleAnimation { 	CCDirector* director = [CCDirector sharedDirector]; 	[director end]; } 

I won’t go through the details of how to create a CCScene and CCLayer – there’s plenty of Cocos2D tutorials out there. The code that you’ll be most interested in is this:

 if(![CCDirector setDirectorType:kCCDirectorTypeDisplayLink]) { 	[CCDirector setDirectorType:kCCDirectorTypeDefault]; }  CCDirector *director = [CCDirector sharedDirector]; EAGLView *glView = [EAGLView viewWithFrame:CGRectMake(0, 0, 1024, 768) 			pixelFormat:kEAGLColorFormatRGB565 depthFormat:0]; [director setOpenGLView:glView]; [director setDeviceOrientation:kCCDeviceOrientationPortrait]; [director setAnimationInterval:1.0/60]; [self setView:glView]; [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGB565]; [[CCDirector sharedDirector] runWithScene:introScene]; 

This basically sets up the Cocos2D environment, and starts running your CCScene. And that’s it! I presented this UIViewController modally, but I expect you should be able to use this method within a subview just as easily.

Combining UIKit and Cocos2D does suffer a performance hit, but I’m not doing anything overly complex here, so it wasn’t a problem. I’m also not sure if this is entirely the correct way to do things, but again – it works, so I’m happy.

Cocos2D has a heap of really nice effects, with a great community behind it. If you need some extra pizazz in your app, it might just be the way to go.

SOURCE LINK from iDevBlogADay

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

WP Like Button Plugin by Free WordPress Templates