Question

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!

Was it helpful?

Solution

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)

OTHER TIPS

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"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top