iOS7 what is the correct way to create and use SKTextureAtlas?

610 views Asked by At

I'm not sure of the underlying implementation of the texture atlases, so my question is - what is the correct way to deal with pulling textures out of them? I need to loop through various atlases and pull out 64 random textures.

Create a static atlas and reuse the reference to pull out textures?

static SKTextureAtlas *monsterAtlas;
static int monsterCount;

monsterAtlas = [SKTextureAtlas atlasNamed:@"monsters"];
monsterCount = [monsterAtlas textureNames].count;

//pull out a random texture
NSString* textureName = [[monsterAtlas textureNames] objectAtIndex: arc4random()%monsterCount];
SKTexture* texture = [monsterAtlas textureNamed:textureName];

-OR-

create a new atlas every time I need a texture?

SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"monster"];
SKTexture *f1 = [atlas textureNamed:@"monster-walk1.png"];

The reason i'm asking is that with the first approach, my code may be quite unweildy, as I would have 10+ atlas references created. Will this use too much memory? With the second approach I'm concerned that I will be doing a lot of extra work creating an atlas each time I execute a loop iteration. How should I do this?

1

There are 1 answers

0
CodeSmile On BEST ANSWER

Create each atlas once and keep a reference to it.

It's not unweildy if you write a class that manages atlases and gives you access to individual textures.