Random Wrinkles with Cocos2D-Swift

Cocos2D-Swift is open source, so if you find an issue with it you can look at the engine code and you have a chance of fixing it. But be aware that the more you change the higher the maintenance cost. Cocos2D is frequently updated, so you have to be careful to redo/merge any fixes you've made to the engine on every update.

Not only is Cocos2D frequently updating, but it's forked into several versions (Cocos2D-x, Cocos2D-JS, Cocos2D (Python), Cocos2D-Swift, Cocos2D-XNA for example). This is great, but there's a downside every developer hits. It's hard for the internet to document the software in all it's variations, how to use it's features, the pitfalls, workarounds, and best practices. As a user, it's harder to find relevant information about your favourite flavour of Cocos2D as any reference to it on the web may well be incompatible.

I've had to delve around in the internals of Cocos2D to fix a few issues I found along the way while making Shift Escape, but I've minimised the changes - one-liners usually.

These are the random wrinkles that just popped up unexpectedly during development using Cocos2D v3.3 (SpriteBuilder v1.3.6):

1. CCLabelTTF

I wanted to use a custom TTF font for Shift Escape, but this caused the game to crash on iOS 6. Turns out the logic is the wrong way around in function:

+(NSString*) registerCustomTTF:(NSString *)fontFile

I changed the following line to be != (does-not-equal) rather than == (equals):

BOOL needsCGFontFailback = [[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] == NSOrderedAscending;

That said, the smarter answer here is: don't bother spending time supporting iOS 6 any more. It's a tiny percentage of users these days.

2. CCDrawNode

CCDrawNode started as a simple debugging facility to draw lines and polygons, but has become so useful it has been expanded and solidified a bit. However some immaturity remains. When drawing a polygon, the code internally 'extrudes' (moves) the vertices supplied before drawing them. This makes it very difficult to align polygons, for example to properly butt up against each other, and (worse) it means that long thin polygons are rendered incorrectly, corrupting the visuals. I needed a function that draws the polygon properly with vertices I specify, so I changed function:

-(void)drawPolyWithVerts:(const CGPoint *)_verts count:(NSUInteger)count fillColor:(CCColor*)fill borderWidth:(CGFloat)width borderColor:(CCColor*)line;

In particular (for minimal change) I just commented out one line:

extrude[i] = (struct ExtrudeVerts){offset, n2};

3. CCTableView

When archiving Shift Escape for release, the validation step came back warning about a function:

- (float) tableView:(CCTableView*)tableView heightForRowAtIndex:(NSUInteger) index;

This conflicted with the naming of a similar iOS function. To avoid the validation warning, I renamed it to ccTableView, and fixed up the references to it (changing CCTableView.m/.h), as suggested here.

4. Content Scale Factor

This didn't work for me as expected for iPhone 6 and iPhone 6+ out of the box. Nodes were not scaled as expected. It turns out Cocos2D has a global scale factor that can be manually set. I ended up setting specific values for these devices in the AppDelegate on startup. To detect which device was running, I used the macros here.

if( IS_IPHONE_6 )
        [[CCDirector sharedDirector] setContentScaleFactor: 2.5f];

5. Additionally

This is not a bug, but I found the following to be a useful addition to allow for changing the Anchor Point of a node without changing position on screen: CCNode+AnchorPos.m/h