Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SKTextureAtlas how to load textures in order

Tags:

ios

sprite-kit

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: [email protected]
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: [email protected]
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: [email protected]
2013-12-04 09:56:54.409 Santa Game[41611:70b] texture: [email protected]
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: [email protected]
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: [email protected]
2013-12-04 09:56:54.411 Santa Game[41611:70b] texture: [email protected]
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: [email protected]
2013-12-04 09:56:54.456 Santa Game[41611:70b] texture: [email protected]
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: [email protected]
2013-12-04 09:56:54.457 Santa Game[41611:70b] texture: santaAndSleigh.7.png

Thanks!

like image 894
Mike S Avatar asked Dec 04 '13 00:12

Mike S


2 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.

like image 72
Andrey Gordeev Avatar answered Oct 23 '22 02:10

Andrey Gordeev


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

like image 27
Andrew Obukhov Avatar answered Oct 23 '22 02:10

Andrew Obukhov