One Game, Many Frameworks: Cocos2D

Next on the list was supposed to come LibGDX, but the bug with Sparrow bothered me too much and I wanted to see the game actually running with a clean bill of health from Apple Instruments. Plus, I wanted to run comparisons with Sparrow.

There isn’t much one needs to do in order to change from the Sparrow version. Here are the main changes:

Changes from Sparrow to Cocos2D

1- I had to use SpriteBatchNodes as containers for the textures so that the number of opengl texture bindings are reduced during rendering. So I decided to add a Container property inside each screen, and that container is a SpriteBatchNode object. That way I can associate one texture atlas per screen if I need.

2- I created an ImageCache object as a helper object to grab Textures and ready-to-use Sprites. But mostly so I could keep referencing that object in my logic, something I wouldn’t need to do in Cocos2D, since the texture cache in Cocos2D is a singleton and can be accessed anywhere in the code.

3- The death animation had to be done with animation objects instead of a MovieClip. I had to use a CCSequence so that I could have the animation and a callback when the animation had finished playing.

4- It’s harder to create an empty texture with Cocos2D than it is with Sparrow. Again the reason is also related to the way the frameworks handle the reduction in texture binding. When you work with a SpriteBatchNode you have to add only textures listed in the atlas bound to that batch node, you can’t draw on it or add “foreign” textures, even if empty ones. So the solution in Cocos2D is to create empty textures inside the atlas and use those instead of creating an empty texture.

5- The same thing goes for drawing squares. In Sparrow you can use a Quad to do that, and I used it to create the backgrounds, but with Cocos you would have to create a special layer so you can use the drawing api to draw on that layer. It cannot be the SpriteBatchNode layer you use with the textures. In the end, I resolved the issue by making the Screen object inherit from CCLayerColor, so I could paint the entire screen with a fill color.

6- In order to animate the time bar, I used a texture and changed the clipping (masking) of that texture, since I could not draw a rectangle on that SpriteBatchNode.

7- I used TexturePacker to export the sprite sheet formatted to Cocos2D instead of Sparrow.

8- And of course SPImages became CCSprites, SPTextures became CCSpriteFrames, and SPMovieClip became a combination of CCAnimation, CCSequence, CCCallFunc.

With the AS version done and tested, it took two afternoons to change the game to Objective-C and Sparrow. Then with that done and tested, in one brief session I had the game running in Cocos2D. It was that easy!

In the next pages I will list the main classes done in Cocos2D and their Sparrow version too.

Download the source code