Cocos2d and ARC
The ARC compatibility has already been merged in the latest branch of cocos2d – the 2.0 beta. And that’s great of course
However “ARC compatible” turns out to be a broad definition. To use cocos2d in an ARC project (cocos2d being ARC compatible) you still have to disable ARC for cocos2d. Yup just like in the previous major version. And it’ll still not compile, but you have to do couple of tweaks in the code to get it running. Pity in the end – but hey we get the awesome cocos2d for free, so whatever comes it’s all good!
I actually ported a big cocos2d project to ARC last week using the 1.1 branch of Cocos2d and it was an okay process (EDIT: btw I looked into using 2.0 beta and ARC and it seems to have some issues), took several hours but most of the time I spend in cleaning up my own code (quite big project) for ARC (mostly removing autoreleases and casting delegates to id ). So it’s not scary if you have the extra time - converting to ARC is embracing the future
However cocos2d actually being ARC compatible would’ve been great …
With that said, here’s few links to help you out converting your game to ARC
1) Here’s Riq’s notes on the ARC compatibility:
http://www.cocos2d-iphone.org/wiki/doku.php/release_notes:2_0_0#new_features_improvements
2) Here’s the instructions (now official as recommended by Riq) how to change cocos2d to run with your ARC project:
http://www.tinytimgames.com/2011/07/22/cocos2d-and-arc/
3) Here’s a list of what I myself did encounter during converting my game to ARC:
- all autorelease calls should go
- Box2D – setting the body’s sprite should get a cast like:
b2BodyDef carBodyDef; carBodyDef.userData = (__bridge void*)carSprite; |
- your custom dealloc will still get called, but … you cannot call [super dealloc] inside it
- oh yeah, no properties’ names should start with”new” … that felt stupid as I had a property “newDirection” and spent good few minutes to come up with a new name for it, which afterwards felt out of place for ever
- atomic properties cannot have custom getter/setter methods- just make them non-atomic and no need to change in the code
- if you’re assigning your class as a delegate often you’ll have to cast it to (id)
- there’s of course more … but I think these were the most common in my project, so good luck
cheers, Marin
The post was originally published on the following URL: http://www.touch-code-magazine.com/cocos2d-and-arc/
·



Of course one could also just download Kobold2D (http://www.kobold2d.com) and get started with Cocos2D & ARC right away. All current (15) and future example projects have ARC enabled by default, you don’t need to change a single line of code.
Yes Steffen, so I hear
[...] Cocos2d and ARC: http://www.touch-code-magazine.com/cocos2d-and-arc/ [...]