1

Whenever I use

sprite = [CCSprite spriteWithFile:@"image.png"];

Cocos2d has always been intelligent enough to look for "image.png" on non retina devices and "image@2x.png" for retina devices.

But whenever I try to dynamically build out the sprite filename string i.e.

    int random = arc4random() % 2;

    switch (random) {
        case 0:
            color = @"blue";
            break;
        case 1:
            color = @"green";
            break;
        default:
            break;
    }

    filename = [[NSString alloc] initWithString:color];
    filename = [filename stringByAppendingString:@"Square.png"];

    // Sprite should now be either "greenSquare.png" or "blueSquare.png"
    sprite = [CCSprite spriteWithFile:filename];

All of a sudden, my @2x files are being completely ignored - and cocos2d is only looking for files that are === "greenSquare.png" i.e. "greenSquare@2x.png" is never picked up, and if "greenSquare.png" doesn't exist (I was only testing with retina displays at the time) the entire app crashes.

Any idea why this is happening and what I can do to solve this?

(Ideally without having to manually check device retina status and then manually appending the @2x - since I know thats a possible solution it just doesn't seem like the right one).

Thanks!

2 Answers 2

6

cocos2d doesn't use @2x naming scheme. Instead it uses suffix -hd for iphone retina images, -ipad for ipad images, -ipadhd for ipad retina images.

Example: "Square.png" and "Square-hd.png"

You can also enable fallback suffixes. When enabled it will try to search for the following suffixes in the following order until one is found:

  • On iPad HD : iPad HD, iPad, iPhone HD, Resources without resolution
  • On iPad : iPad, iPhone HD, Resources without resolution
  • On iPhone HD: iPhone HD, Resources without resolution
  • On Mac HD : Mac HD, Mac, Resources without resolution
  • On Mac : Mac, Resources without resolution

(as documented in CCFileUtils.h file)

1
  • 1
    That worked perfectly! I wonder why the original test code i.e. sprite = [CCSprite spriteWithFile:@"image.png"]; automatically detected both image.png and image@2x.png
    – Salman
    Jul 12, 2013 at 18:43
1

the standard cocos2D for the retina images is a sufix -hd if your image named "greenSquare.png", the retina for cocos2D need named "greenSquare-hd.png" but you can change this standard and tell cocos2D use the sufix @2x , you can do that modifying the AppDelegate.m

search the lines

CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils];
[sharedFileUtils setiPhoneRetinaDisplaySuffix:@"-hd"];

and change the sufix @"-hd" to @"@2x" , so your code maybe look like:

[sharedFileUtils setiPhoneRetinaDisplaySuffix:@"@2x"];

a best practice is change the name of all of your images, replace the "@2x" to "-hd"

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.