1

I have an atlas with a bunch of tiles and i am trying to load them into memory using SKTexture and SKTextureAtlas but it is not working. I use the following code to load them:

NSString *atlasName = [NSString stringWithFormat:@"Tiles"];
SKTextureAtlas *tileAtlas = [SKTextureAtlas atlasNamed:atlasName];
NSInteger numberOfTiles = tileAtlas.textureNames.count;
backgroundTiles = [[NSMutableArray alloc] initWithCapacity:numberOfTiles];
for (int y = 0; y < 5; y++) {
    for (int x = 0; x < 9; x++) {
        int tileNumber = y*9 + x + 1;
        NSString *textureName = [NSString stringWithFormat:@"tile%d.png",tileNumber];
        SKSpriteNode *tileNode = [SKSpriteNode spriteNodeWithTexture:[tileAtlas textureNamed:textureName]];
        CGPoint position = CGPointMake((0.5 + x)*_tileSize - _levelWidth/2,(0.5 - y - 1)*_tileSize + _levelHeight/2);

        tileNode.position = position;
        tileNode.zPosition = -1.0f;
        tileNode.blendMode = SKBlendModeReplace;

        [(NSMutableArray *)backgroundTiles addObject:tileNode];
    }
}

Then i use this code to add them to my scene:

- (void)addBackgroundTiles
{
    for (SKNode *tileNode in [self backgroundTiles]) {
        [self addChild: tileNode];
    }
}

The problem is it doesnt load the correct texture for a tile or find the texture at all. What I end up with is this (ignore the blue circle): https://i.stack.imgur.com/g39BF.png

Here is my tile atlas: http://snk.to/f-ctp5yhpz

EDIT: I am using NameChanger(www.mrrsoftware.com/MRRSoftware/NameChanger.html) to rename all my tiles, can it be that program that messes up my pngs? as far as i can see they are in the correct order after i have renamed them.

Marcus
  • 97
  • 1
  • 13
  • I can't see the name changer being your problem, as long as your doing it after your frames have been rendered (created) and before you add them to the atlas. The name changer won't be rewriting any of the files, just changing the file names as you would do if you were renaming them all by hand. Do of course make sure they have the right extension ".png" and the @2x prefix if that's what you are using. – fuzzygoat Mar 02 '14 at 07:34
  • I didnt have the @2x extension but i added that now and the problem still remains. – Marcus Mar 02 '14 at 13:12

2 Answers2

0

Why the double for loops? Are you saving the backgroundTiles array as a property?

I've had this occur recently and the only fix that worked was: [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:@"someTile.png"]]; The textureWithImageNamed always gets the right one.

So try:

SKSpriteNode *tileNode = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:textureName]];
Norman G
  • 759
  • 8
  • 18
  • i need the x and y to calculate position, here is how i save the array: static NSArray *backgroundTiles = nil;, not as a property – Marcus Mar 01 '14 at 20:06
  • SKTextureAtlas doesnt have the textureWithImageNamed method – Marcus Mar 02 '14 at 12:56
  • I'm wondering if its my pngs that are wrong in some way, but they look fine to me, I'm really confused. – Marcus Mar 02 '14 at 13:09
  • Yes I meant: SKSpriteNode *tileNode = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:textureName]]; – Norman G Mar 03 '14 at 10:24
0

Solution

Editing my answer to point out that the solution is in the comments below this answer.

It turned out that the issue was caused by Xcode not rebuilding the atlas after the image files were renamed outside of Xcode (presumably by the file changed OP mentioned).

By cleaning and rebuilding the project, all the texture atlases were built again, and OPs code started working.


Original answer

Two things to double-check:

  1. Is your .atlas added to your project as a folder or a group? It must be a folder (blue icon in Xcode, instead of yellow).
  2. After adding Tiles.atlas to your project, you must also enable atlas generation in Xcode settings.

See here for a similar issue: How to create atlas for SpriteKit. I linked to Apple documentation on incorporating texture atlases into your projects which has a detailed step-by-step instruction on enabling atlas generation.

Community
  • 1
  • 1
Janis Kirsteins
  • 2,128
  • 16
  • 17
  • My atlas is added as a folder in my project(blue icon) and i have the correct settings for atlas generation in xcode. I do exactly as he do in the link you posted, "How to create atlas for SpriteKit", except i have spriteNodeWithTexture instead spriteWithTexture like he wrote. – Marcus Mar 02 '14 at 13:07
  • In that case, maybe SpriteKit doesn't keep a strong reference to the atlas internally. Try moving SKTextureAtlas* to a strong property of the scene, and see if that helps. – Janis Kirsteins Mar 02 '14 at 13:29
  • tried that. I did: @property (nonatomic,strong) SKTextureAtlas *tileAtlas; instead. The problem still remains and my scene looks exactly the same as the image i linked to in the original question. – Marcus Mar 02 '14 at 13:42
  • I could reproduce your issue by building a fresh atlas, then renaming tiles outside of Xcode. The atlas does not get re-built and the old names are still relevant. I suspect this is what happened to you, since you mentioned a name changer utility. Try cleaning the project and rebuilding it. – Janis Kirsteins Mar 02 '14 at 14:08