COCOS2D, Retina Display and UIImage

I’ve been having a small issue with my iOS game development for a while that’s using the COCOS2D framework. Since the start of development I wanted to make sure I supported the retina display. For the most part, I had no problem adding the double resolution images with the -hd filename suffix (COCOS2d doesn’t use iOS’s standard @2x suffix).

I only started running into an issue when I tried swapping out a texture on a sprite with another image on the fly. I was using the following code to create the texture:

[[CCTexture2D alloc] initWithImage:[UIImage imageNamed:@”fullstar.png”]];

When I replaced the sprite’s texture with the one created above it was always using the lower resolution image, not the fullstar-hd.png image.

Turns out UIImage is an iOS class and not a COCOS2D class so it follows iOS’s retina display rules. In this case, I need to have an image named fullstar.@2x.png. Once I created that image and added it to the project my retina resolution image was loaded.

So, what did I learn here? Make sure you know who made your objects! If it’s a COCOS2D object (prefixed with CC) use the -hd suffix for images. If it’s an iOS object, @2x. Simple mistake, and as per usual makes perfect sense in hindsight. Also, this probably would have been avoided by using a spritesheet instead and just treating the changing of the image as an animation.

Leave a Reply

%d bloggers like this: