mredig
mredig

Reputation: 1815

SpriteKit SKTextureAtlas atlasWithDictionary method

I'm not sure if I'm doing this wrong or if there's something broken with the "atlasWithDictionary" method.

This is how I used it:

NSArray* imageNames = @[ @"image1", @"image2", @"image3", @"image4"];
NSMutableDictionary* tempDict = [NSMutableDictionary dictionary];

for (int i = 0; i < imageNames.count; i++) {
    UIImage* texture = [UIImage imageNamed:imageNames[i]];

    [tempDict setObject:texture forKey:imageNames[i]];
}



SKTextureAtlas* atlasFullOfTextures = [SKTextureAtlas atlasWithDictionary:tempDict];

and then later in my code, whenever I would do

SKTexture* tex = [atlasFullOfTextures textureNamed:@"image1"];

I just get a nil object. I did a bit of troubleshooting and found that

NSArray* arrayOfNames = [atlasFullOfTextures textureNames];

returns an empty array.

Also, I know that the images are loading fine as I temporarily made the dictionary public and successfully made SKTextures from the UIImage objects.

Does anyone have any idea what's happening?

Upvotes: 0

Views: 667

Answers (1)

CodeSmile
CodeSmile

Reputation: 64477

From the documentation:

The keys in the dictionary represent the names of the individual textures. The associated object for each key can be:

  • An NSString object that contains a file system path to a file that contains the texture

  • An NSURL object that contains a file system path to a file that contains the texture

  • A UIImage object

  • An NSImage object

You are not providing a path/url to the image, you just use image names with no way of telling where they might be. You will probably also have to specify the file extension as Sprite Kit probably won't try to guess it. If these are textures obtained or created at runtime, they will not be in the bundle so you have to specify what the path to each texture is as well (usually in the appdata or documents folder).

If these images are in the bundle, you probably have to specify the bundle directory. But there's little reason not to have the atlas be created by Xcode at compile time in this case and then using atlasNamed:.

Upvotes: 1

Related Questions