Cocos2D and UIKit working together

I have been refactoring BocceBeers lately, and as a fan of tools such as Interface Builder, I didn’t have the time to really learn the ins and outs of CocosBuilder, and I already had working gameplay code, so I wouldn’t really need to rebuild the game in CocosBuilder because that would be a make-work project.

What I did need to do is make User Interfaces for the iPad version of the game that I am making.  So I thought, now that I’m an experienced App Developer, can I not just use UIKit and make NIB files?

The answer is yes.  I decided to go about making a general solution (demo project) for any of  you seeking to use UIKit for menu systems, but then leverage the power of Cocos2D for your gameplay.

I also created an asynchronous scene loading class which you can check out as well.

Please check out the demo project on github here.

Indie Game Development and BocceBeers v2.0

Now that I’ve got a bit of down time and am on holiday, I thought I’d dust off the code of my very first software project in my career BocceBeers, and see if I can give it some of the upgrades it’s been screaming for since iOS 3.0.

Refactoring old code is a funny endeavour!  Seeing as BocceBeers was the first thing I ever wrote, completely on my own, not much help other than forums, I’m amazed at 2 things:  That they game runs as well as it does, and how messy the code was!

Basically, I’ve had to majorly rewrite, rename, convert to ARC, ensure better decoupling and encapsulation just to get the game more or less running again.  That was the boring part.  But then it moved on to more interesting things…

My goals for this upgrade are to make it universal and playable on the iPad, adding retina support, and make it possible to have turn-based play over the internet.  If I find time, I will also look into incorporating an instant replay system I had been working on.

(On a cheesy note, I should add that the inspiration to do this now as that I gave my parents an iPad mini recently and back in 2010 when I wrote the original BocceBeers while home at my parents’ on holiday, my mom was my guinea pig game tester.  So now that she has an iPad, the proud mom that she is, asked me: “Can I play your game on this?”  So I thought, I’m gonna improve this game!)

As such, having become more of a UIKit expert than Cocos2D (on which BocceBeers 1.0 is written), I thought I’d rip out all the front end classes implemented in Cocos2D and use UIKit.  This makes it a lot faster to me to write UI Code, and UIKit is really an amazing framework that we all just accept but probably rarely marvel at the technological genius of Apple Engineers.  Now that I’m 3 years more experienced, I’m finding Cocos2D to be a great framework, but there are some oddities about it, just sometimes even down to strange naming conventions.

Anyhow, the whole point of this post is to say I’m loving this job, and I think my true calling in life is probably to be an indie games developer because of the total freedom it allows and it’s highly creative and most importantly fun!  That said, any iOS development has been fun.

Cocos2D Revised Template – Easy-ARC, Unit Tests, Cross-Ref’d Source

I don’t know about you guys, but on the one hand I’m grateful for all the people who make it easy to get up and running with either a UIKit based project or a Cocos2D based project in Xcode. Because I’ve just spent literally hours ripping my hair out in frustration, stuck on configuration details and so on. I hate that; trying to fumble through a task, really not knowing what causes it to fail and in the end you get it working and really not know why it did stop failing. (Talk about a cry for mentor help!)

So, this is what I want to do:

* I want the functionality of a Cocos2D template, but I want to cross-reference the Cocos2D-iOS project and not have the source there already (i.e. if I wanna update my source, it should be easy)

* I want it set up for ARC

* I want to have unit tests

Unfortunately the current templates don’t provide this. So, here’s how I got it working:

* Create a new Project, using the Cocos2D iOS Template

* Now go to the App Target’s Build Settings and notice what might be interesting here (in the event you wanted to work without templates):
– Other Linker Flags (-lz -ObjC)
– Location of the Info.plist file
– Header Search Paths (where to locate the kazmath headers)
– Location of Prefix Header
– Preprocessor Macros

* Double check some Project Build settings
– iOS Deployment target (could be 5.0 at this point)
– Objective-C Automatic Reference Counting should be set to YES, and make sure this is also the case in your App Target

* We will now delete the libs folder from the Project and send it to the Trash, but before that, notice what libs are in there: cocos2D, cocosDenshion, and kazmath. Ultimately these are the ones that our project will depend on. Now delete that libs folder and send to trash.

* Now we want to cross-reference the cocos2D source, so that it can be managed separately from our App’s code. The way I like to do this now is with git submodules (as I don’t write any code without it being in a git repository, local or remote, as long as there’s some source control!) So for that we go to the command line in the root of our project and type:

git submodule add cocos2d_src

Which will clone that repo into this repo, and put it in the folder called cocos2d_src.

(At this point you may want to create an appropriate .gitignore file for your repo if you haven’t done so already – github provides a decent one for Objective-C or see the bottom of this post for something you can copy-paste)

* Now, in that cocos2d_src (once it’s finished downloading!) folder, drag the cocos2d-ios.xcodeproj file into your project, thereby creating a cross-project reference. Another reason why cross-project references are good is that they compile separately, which means we don’t care if that project is ARC or not, because it gets compiled separately. We want ARC, so it’s good to keep cocos2D source out of our Project’s source, so you don’t have to turn on ARC for half of your project, and off for the other half.  Let them compile separately.

* Now we need to be able to work with that source. As I mentioned, we need to be able to use cocos2D, cocosDenshion, and kazmath. In your PROJECT‘s (You put it in the project’s settings to that the unit tests will also have access to them… does anyone know a ‘cleaner’ way to do this?) build settings, go to Header Search Paths and add 3 entries:

$(SRCROOT)/cocos2d_src/cocos2d                          (recursive)
$(SRCROOT)/cocos2d_src/external/kazmath                 (recursive)
$(SRCROOT)/cocos2d_src/cocosDenshion/cocosDenshion      (recursive)

The next step is to set up the dependencies to this other project. In your Build Phases, add target dependencies to cocos2d, CocosDenshion, and kazmath. In Link Binary With Libaries, add libcocos2d.a, libCocosDenshion.a, and libkazmath.a

Now, in your Project’s Precompiled Header file (Prefix.pch), add #import “cocos2d.h” underneath the UIKit and Foundation framework imports. For those of you a bit new to this, this is a way of saying “make this framework available everywhere, as if I import this in each header file I make”. It’s also a quick check to see if everything has worked so far. Build and run the project and hope it succeeds.

If yes, then great. If not… Oh yeah, you’ll get errors due to ARC being enabled and some code is non-ARC. Either delete the HelloWorldLayer and Intro Layer classes from your project, or remove all lines of code with “release” statements, and remove all dealloc method implementations. Then you also have to update your main.m file as it uses new syntax regarding Autorelease pools:

– replace old non-ARC main.m implementation with ARC main.m implementation:

int main(int argc, char *argv[]) {
  @autoreleasepool {
    return UIApplicationMain(argc, argv, nil, @"AppController");

Otherwise if it still doesn’t work… hmm… double check the steps above? Don’t you just hate those tutorials when it doesn’t work as written?

Now then, this is Checkpoint ONE of the tutorial. Now we have an ARC-ready project that uses Cocos2D source that you can keep up-to-date as it’s checked out from the github repository. Convenient!

Next, onto adding Unit Testing. The next bit was inspired by this post, summarised below:

* Click on your Project to get to its settings -> Add Target -> iOS -> Cocoa Unit Testing Bundle
* Name the new target something appropriate -> “MyAppTests”
* Go to the Build Phases tab of your UNIT TEST target, and create a dependency to your App Target
* Under Build Settings, set Bundle Loader to:


* Set the Test Host to


* Go back to your APP TARGET, and set Symbols Hidden by Default to NO

In addition, you may want to set your UNIT TEST target’s pre-compiled header .pch file to be the same as your App Target, but that’s up to you and your desired configuration.

So, if everything’s gone right, you’re up and running and good to go! You may want to commit this to a repo with a specific tag, so you can check that out in the future and save yourself some time.


You could add ChipmunkPro to your project, if you do it’s pretty straightforward.  Import the folder into your project (with static lib .a file) and add ObjectiveChipmunk.h to your .pch file.  Ooops, but it won’t compile!  You have to link the ImageIO.framework to your project.

EXTRA AWESOME TIP: Use the following as your .gitignore file BEFORE you do any commits!!! Otherwise it’s a pain in the ass for the git beginner to deal with it later, especially when switching branches and dealing with Xcode files…

# Xcode

#Xcode files
#This ignores everything inside a *.xcodeproj except the project.pbxproj

# OSX Noise

Easy-ARC Cocos2D Project

I’m just beginning to get back into Cocos2D (for fun, I want to try to create an instant replay system), and before that can happen, one has to set up a project.

Installing the templates is ok, but there are 2 main drawbacks: It does a straight import of the source files (as opposed to referencing the project so you can keep your source up-to-date), and the entire project is still set to non-ARC.

So… what to do?

EDIT: I improved this post and it is here: