SKTextureAtlas textureNamed does not automatically find texture suffixes @ 3x in XCode 6 GM Seed

I had NSLog my textureAtlas that it loaded all my 3x.PNG files loaded, but it returns the missing texture by calling textureNamed:

#define TEXT_MAINMENU @"mainmenu"
SKTextureAtlas *textureAtlas = [SKTextureAtlas atlasNamed:@"414x736"];
NSLog(@"atlas: %@", textureAtlas);
SKTexture *_MAINMENU = [textureAtlas textureNamed:TEXT_MAINMENU];
NSLog(@"texture from skatlas: %@", _MAINMENU);

      

The output is here:

atlas: '414x736' 83 textures: (" mainmenu@3x.png " (279 x 279) "," ' winreplay@3x.png ' (873 x 131) "," bonus128@3x.png "(279 x 279) "," maincake128@3x.png "(279 x 279)", "' bonus512@3x.png ' (279 x 279)", ........

scat texture: "MissingResource.png" (128 x 128)

However, when I get a texture by textWithImageNamed (and not by SKTextureAtlas textureNamed) and it returns @ 1x.PNG to me?

SKTexture *_MAINMENU2 = [SKTexture textureWithImageNamed:TEXT_MAINMENU];
NSLog(@"texture from sktexture: %@",_MAINMENU2);

      

The output is here:

texture from sktexture: 'mainmenu' (90 x 90)

Of course, I also had @ 1x @ 2x PNG for the same code that works well, and I'm wondering why my Atlas / texture doesn't automatically find my @ 3x.PNG? Is there something wrong with me and how to fix it?

+3


source to share


1 answer


You are probably getting the "MissingResource" message because you are not accessing the texture atlas with a valid key. When you manually create a texture atlas, you usually use it atlasWithDictionary:

like this:

NSDictionary *testDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"mine@1x.png", @"mine64", @"mine@2x.png", @"mine128", @"mine@3x.png", @"mine256", nil];

SKTextureAtlas *testAtlas = [SKTextureAtlas atlasWithDictionary:testDictionary];

      

my64 is 64x64 png, mine128 is 128x128 png, and my256 is 256x256 png. Obviously, you need unique keys for the dictionary, so you have to manually specify what size of the image you want, depending on the iPhone model, when it's time to place the image like this:

SKSpriteNode *myNode = [SKSpriteNode spriteNodeWithTexture:[testAtlas textureNamed:@"mine256"]];

      

BUT, according to the latest Apple SKTextureAtlas docs:



Discussion Typically, Xcode creates texture atlases at compile time from the image files included in your project. These atlases are compiled and installed inside the application package. However, sometimes the assets required to create a texture atlas are not available at compile time. For example, these assets can be generated or downloaded from the network as procedures. However, you still want texture atlases to reduce the number of state changes required by the hardware. You can use this method to create the atlas at runtime. This is a potentially expensive operation and is best performed when the game loop is not running.

As I understand it, Xcode will automatically generate all the required texture atlases. With that in mind, adding these 3 images to the project: mine@1x.png , mine@2x.png , mine@3x.png , and then using:

SKSpriteNode *myNode = [SKSpriteNode spriteNodeWithImageNamed:@"mine.png"];

      

will result in the correct use of the image based on the screen resolution.

An alternative is to use an application like TexturePacker to create your own texture atlases and add them to your project.

+1


source







All Articles