Cocos2D doesn't make use of the Automatic Reference Counting (ARC) features now available in the new compiler (with XCode 4,) and it's a little fiddly to make it work.
We cracked it, with a little help from Tiny Tim Games (do feel free to follow their tutorial instead.) We thought we'd give you a quick step-by-step guide to getting it working, (it's also for future us too.)
Get Cocos2D
Grab this branch of the project: https://github.com/cocos2d/cocos2d-iphone.
- Either download it, or type this into Terminal (into a sensible directory): git clone https://github.com/cocos2d/cocos2d-iphone.git
- It's pretty massive (over 50MB), so do it just before you need a cup of tea
Create a new Blank Project
Create a new blank project, and be sure to check (turn on) the "Use Automatic Reference Counting" option.
Do something that requires Cocos2D in your new project
- Open your AppDelegate.m file and add an import for cocos2d.h
- Add this snippet of code to the 'application:didFinishLaunchingWithOptions:' method:
CCScene *scene = [[CCScene alloc] init];
[scene cleanup];
- Try to build your project, notice that it fails with an 'Apple Mach-O Linker" error. This is because it's trying to use CCScene class which we haven't added a reference for yet.
Include Cocos2D
XCode supports 'cross-project referencing', and this is the best way to include Cocos2D. In Finder, locate the 'cocos2d-ios.xcodeproj' file, and drag it into your new project.
- You should see that the project gets included wholesale (literally like a sub-project) in XCode.
- I then like to drag the Cocos2D project into the Frameworks folder to keep things clean.
Link to Cocos2D and tweak some settings
- Select your project in XCode
- Choose the "Build Phases" tab
- Expand the "Link Binary With Libraries (x items)" section
- Click the + symbol to add another library
- From the "Workspace" section, select 'libcocos2d.a"
- Click "Add"
- If it's red, don't worry, it has still worked
- Repeat the same for the following frameworks: OpenGLES, libz, QuartzCore
- NOTE: You will likely need to add more frameworks as you use more features of Cocos2D, but for now, this works.
Then nip over to the "Build Settings" tab:
- Set "Always Search User Paths" to "YES"
- Add the Cocos2D project path to the "User Header Search Paths" property, ensuring it's recursive
- In my case, I set it to: "/Users/matryer/Work/cocos2d/**"
Build again
- Build your project again and notice that all works