0

I have issue when use more than one atlas.

For example I have main_menu.atlas and game.atlas with images for these scenes. Before main menu scene appears I prepare atlas for it ( [SKTextureAtlas atlasNamed:@"main_menu"] ) and all works fine. But after when I started a game and also prepare game atlas ( [SKTextureAtlas atlasNamed:@"game"] ) in a game I see only empty nodes (rectangles with red crossing). There is no exeptions or warnings - all is OK.

And when I moved all game assets into main_menu.atlas and removed game.atlas all works fine - I see sprites in a game. But I want to separate atlases for performance optimization.

I use my own helper for SpriteKit textures management. It loads atlases and returns textures which I need. So I have these methods:


- (void) loadTexturesWithName:(NSString*)name {
    name = [[self currentDeviceString] stringByAppendingString:[NSString stringWithFormat:@"_%@", name]];
    SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:name];
    [self.dictAtlases setObject:atlas forKey:name];
}

- (SKTexture *) textureWithName:(NSString*)string {
    string = [string stringByAppendingString:@".png"];

    SKTexture *texture;
    SKTextureAtlas *atlas;
    for (NSString *key in self.dictAtlases) {
        atlas = [self.dictAtlases objectForKey:key];
        texture = [atlas textureNamed:string];
        if(texture) {
            return texture;
        }
    }
    return nil; // never returns "nil" in my cases
}

"Clean" not helps. What wrong I do? Thanks in advance.

  • 1
    I'm pretty sure you can only have one texture atlas and for sprite kit this is usually created dynamically : https://developer.apple.com/library/ios/recipes/xcode_help-texture_atlas/AboutTextureAtlases/AboutTextureAtlases.html – GuybrushThreepwood Jan 21 '14 at 15:32
  • Thank you. But for example if I have two sprites which should present on one scene but the first one stored in ~iphone.1.png and the second one on ~iphone.2.png (another sprite sheet) both of this sprite sheets have to be in memory and draw calls count would increased? I think would be better if both sprites would be in one sprite sheet, but method presented by your link doesn't garantee this. – Igor Shaulov Jan 21 '14 at 15:53
  • 1
    I don't think you've got any choice - it'll find the sprite without an issue though. The way to approach this is to put all your individual sprites into your project as outlined in the link and let SpriteKit build the atlas. As far as I can see Sprite Kit is not designed for use with a pre-supplied atlas. If you need this you might want to look at Cocos2d or an OpenGLES solution. – GuybrushThreepwood Jan 21 '14 at 16:05

1 Answers1

1

Let me start by stating:You can definitely use 2+ texture atlases :)

Now to issue at hand:

You are loading menu atlas first (first in dict) then game atlas. When you grab menu textures all is fine. When you go out to grab game texture you first look in menu atlas (No image is available so atlas returns placeholder texture defined in this doc not nil as you expect.

This code should work as desired

- (SKTexture *) textureWithName:(NSString*)string {
    string = [string stringByAppendingString:@".png"];

    SKTexture *texture;
    SKTextureAtlas *atlas;
    for (NSString *key in self.dictAtlases) {
        atlas = [self.dictAtlases objectForKey:key];
        if([[atlas textureNames] containsObject:string]){
            texture = [atlas textureNamed:string];
            return texture;
        }
    }
    return nil;
}

Also, it will work fine without adding .png :)

Dobroćudni Tapir
  • 3,082
  • 20
  • 37