SKTextureAtlas how to load textures in order

Viewed 2086

I've using a SKTextureAtlas (.atlas folder) to store the frames for an animation I'm using. I've numbered the frames like frame.1.png, frame.2.png, etc - there are 10 frames in total.

I noticed my animation looks horrible even though the frames previewed in my graphics program look great. I NSLoged out and found that it is loading the atlas in random order! I assumed it would at least follow the bundle file order. How do I get it to use the bundle order without sorting the array myself. Also do I put the @2x images in with the rest of them or do I create a separate atlas?

SKTextureAtlas *sleighAtlas = [SKTextureAtlas atlasNamed:@"sleigh"];
NSArray *textureNames = [sleighAtlas textureNames];
NSMutableArray *sleighTextures = [NSMutableArray new];
for (NSString *name in textureNames) {
    NSLog(@"texture: %@",name);
    SKTexture *texture = [sleighAtlas textureNamed:name];
    [sleighTextures addObject:texture];
}

Its printing out:

2013-12-04 09:56:54.407 Santa Game[41611:70b] texture: santaAndSleigh.3.png
2013-12-04 09:56:54.407 Santa Game[41611:70b] texture: santaAndSleigh.6@2x.png
2013-12-04 09:56:54.408 Santa Game[41611:70b] texture: santaAndSleigh.8.png
2013-12-04 09:56:54.408 Santa Game[41611:70b] texture: santaAndSleigh.5@2x.png
2013-12-04 09:56:54.408 Santa Game[41611:70b] texture: santaAndSleigh.4.png
2013-12-04 09:56:54.408 Santa Game[41611:70b] texture: santaAndSleigh.9.png
2013-12-04 09:56:54.409 Santa Game[41611:70b] texture: santaAndSleigh.4@2x.png
2013-12-04 09:56:54.409 Santa Game[41611:70b] texture: santaAndSleigh.9@2x.png
2013-12-04 09:56:54.409 Santa Game[41611:70b] texture: santaAndSleigh.5.png
2013-12-04 09:56:54.410 Santa Game[41611:70b] texture: santaAndSleigh.1.png
2013-12-04 09:56:54.410 Santa Game[41611:70b] texture: santaAndSleigh.3@2x.png
2013-12-04 09:56:54.410 Santa Game[41611:70b] texture: santaAndSleigh.6.png
2013-12-04 09:56:54.410 Santa Game[41611:70b] texture: santaAndSleigh.8@2x.png
2013-12-04 09:56:54.411 Santa Game[41611:70b] texture: santaAndSleigh.2@2x.png
2013-12-04 09:56:54.411 Santa Game[41611:70b] texture: santaAndSleigh.2.png
2013-12-04 09:56:54.455 Santa Game[41611:70b] texture: santaAndSleigh.7@2x.png
2013-12-04 09:56:54.456 Santa Game[41611:70b] texture: santaAndSleigh.1@2x.png
2013-12-04 09:56:54.456 Santa Game[41611:70b] texture: santaAndSleigh.10.png
2013-12-04 09:56:54.456 Santa Game[41611:70b] texture: santaAndSleigh.10@2x.png
2013-12-04 09:56:54.457 Santa Game[41611:70b] texture: santaAndSleigh.7.png

Thanks!

6 Answers

Probably textureNames property contains texture names unordered. Instead of using fast enumeration use for(;;):

for (int i=0; i < sleighAtlas.textureNames.count; i++) {
    NSString *textureName = [NSString stringWithFormat:@"santaAndSleigh.%d", i];
    [sleighTextures addObject:[explosion2Atlas textureNamed:textureName]];
}

Though this method would probably not work if you put @2x images into sleighAtlas.

func getTexturesByAtlas(_ atlasName: String) -> [SKTexture] { let atlas = SKTextureAtlas(named: atlasName) return atlas.textureNames.sorted().map { name in atlas.textureNamed(name) } }

Or just sort the textureNames array using sortedArrayUsingSelector: and something like localizedCaseInsensitiveCompare. Might get wonky with the @2x files included however.

Related