Post news RSS Swift for the Win

Since the future of Apple development seems to be the new Swift language, I had a look at working with Cocos2D using Swift.

Posted by on

Cocos2D is a lovely environment to code a 2D game for iOS. There's a lot of functionality there, and its ahead of SpriteKit for the fullness of what is offered. Also SpriteBuilder is a very nice tool now for building levels, setting up animations and generally managing your game data.

I wrote Space Bot Alpha in Objective-C, but my next game, and maybe even Space Bot Alpha 2 if it comes to that, will likely be in Swift.

But even thought pointing a browser at Cocos2d-iphone.org results in you finding yourself at Cocos2d-swift.org (via a redirect) it turns out that redirecting your development efforts with Cocos2D onto the Swift language is not so simple.


I found one person who had written a rough how-to using the "Mix-and-Match" technique which allows you to have the Cocos2D objective-c code just dumped in, cheek-by-jowl with your Swift based project code.

I prefer an approach which uses static libs to separate out the Cocos2D stuff from my code. Any debug switches and whatever else can apply just to that code. Building Cocos2D in Objective-C as a static library, and also its dependent Chipmunk physics, you can then replace the code that loads Cocos2Ds event loop and OpenGL canvas with Swift code that calls into the Cocos2D via a Swift bridging header.

Instructions and a template project are available on my GitHub project.

Cocos2D in Swift Hello World template

With an evening to play, I was able to get a basic re-usable hello world template up and working. From here its simple to load sprites and start hacking on the game using Swift.

To grab this just visit my github repo for Cocos2DInSwift.

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.