Just started development of a new iPhone game and decided to use cocos2d for the game engine (versus the one built for Kascade). Since there are upcoming API changes, I started with the latest 0.9 alpha/beta (latest tips from subversion) since it will likely be released before the game will be completed.
This entry is part 1 of a series that provides a starting point for anyone using cocos2d. This post kicks things off with useful links and setting up your project.
In addition to the main cocos2d site, the following is a list of links that I found helpful in starting development:
- Misc links including the ones above
All the links above refer to the older versions of cocos2d and so all the examples will have to be slightly modified (new in 0.9 is that all classes are now prefixed with CC).
The good news that as of 0.8.1, cocos2d provides XCode templates to start your own project. It installs three templates: basic cocos2d, cocos2d + Box2D, and cocos2d + Chipmunk. Box2D and Chipmunk are two different physics engines (Chipmunk was derived from an early version of Box2D). Box2D is C++ and Chipmunk is C.
Once you select the XCode template, you have a simple example application that runs in the simulator after a build.
One thing to note is that the template copies all the cocos2d files (and its dependencies) into your project directory. This will be problematic if you to update your cocos2d install like I will need to do (especially with the pre-beta) since I would need to recopy all the files again. That sounds painful so there should be a better/easier way... static libraries to the rescue!
Doing some research on the web, these two links provided most of the relevant information to enable making cocos2d a static library.
The first link shows how to create static libraries and use them in XCode for the iPhone. I'm not sure about prior releases, but the 0.9 version comes with static libraries already included as targets in the XCode project file so this link wasn't as relevant for the setup side of things (although it is a great read).
However, it did point out that you would need a version for every device/simulator combination you want to work with. Not ideal. After reading the second article, there is a way to avoid that.
Before I get into that, you should remove the 'cocos2d sources' from your newly created project. Down in the Targets section, remove the 'cocos2d libraries'.
Right click on your main target and Get Info. On the General tab, remove the 'cocos2d libraries' reference from the Direct Dependencies as well as under the Linked Libraries section.
At this point, I followed Clint's steps (from the second article) - except I skipped his first one: Set up a shared build output directory that will be shared by all Xcode projects.
I didn't do this and everything seems to work fine since the referenced project will build based on your current settings. Of course, I just started my development with this so time will tell if I really needed it.
Once you get to the step called Configure the Library Dependencies, Linking, and Header Files, you just copy the subset of libraries from the cocos2d project reference. A good starting point for inclusion: cocos2d. Chipmunk (for physics), libpng, and CocosDenshion (for sound).
A Few Notes
In 0.9 libcocos2d.a actually includes FontLabel so you don't need to explicitly pull that in (if you do you will have an issue with a duplicate symbol).
You will need to make a single change in this version of cocos2d in order to get the libpng.a to work. Open the cocos2d project and for the libpng target, remove (uncheck) the pngtest.c from the libpng static library target. This file has a main which causes a duplicate symbol issue when compiled in with your application.