Question

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.

Was it helpful?

Solution

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 :)

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