2

I'm using Sprite Kit to develop a game. I want it to automatically use needed textures for iPhone, iPad, and large iPhone versions.

Let's imagine I have:
player01-568h.png - player-10-568h.png
player01@2x.png - player-10@2x.png
player01-ipad.png - player-10-ipad.png
player01-ipadhd.png - player-10-ipadhd.png

How do I store these?
Do I need separate atlas for each one?
Do I check for device each time I need to load atlas and load needed, or is there any automatic way?

Dvole
  • 5,725
  • 10
  • 54
  • 87
  • So the question is how to detect different hardware? Look at this: http://stackoverflow.com/questions/2862140/best-way-to-programmatically-detect-ipad-iphone-hardware – juniperi Nov 18 '13 at 17:15
  • @juniperi no, the question is how to avoid if statements with detecting different hardware. Like on regular iOS, make regular and 2x images work automatically. – Dvole Nov 18 '13 at 17:16
  • stick to the Apple resource filename conventions and this will be automatic, for a summary see: http://www.vigorouscoding.com/2012/03/naming-conventions-for-image-resources-in-ios/ I believe you need to create a separate atlas where the atlas folder has the suffix as per convention, but the contained files do not use any suffixes. – CodeSmile Nov 18 '13 at 21:32

1 Answers1

3

I found a useful method which returns a proper texture atlas for the current device.

Specify

#define IS_WIDESCREEN ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON ) 

on the top of the scene and add this method:

- (SKTextureAtlas *)textureAtlasNamed:(NSString *)fileName {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {

        if (IS_WIDESCREEN) {
            // iPhone Retina 4-inch
            fileName = [NSString stringWithFormat:@"%@-568", fileName];
        } else {
            // iPhone Retina 3.5-inch
            fileName = fileName;
        }

    } else {
        fileName = [NSString stringWithFormat:@"%@-ipad", fileName];
    }

    SKTextureAtlas *textureAtlas = [SKTextureAtlas atlasNamed:fileName];

    return textureAtlas;
}

Now all you need to do is to name your texture atlases properly.

iPhone 3.5-inch: MyAtlas.atlas

iPhone 4-inch: MyAtlas-568.atlas

iPad: MyAtlas-ipad.atlas

Sorry, can't remember where did I get it.

Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
  • 1
    Looks like it came from http://www.raywenderlich.com/49695/sprite-kit-tutorial-making-a-universal-app-part-1 – AW101 Dec 26 '14 at 15:58