Search code examples
iosobjective-csprite-kitsktexturetexture-atlas

Textures from texture atlas are not loading or appearing


For some odd reason textures from my texture atlas are not loading. I have no idea why.

Below is how I typically declare/code a texture

     -(SKSpriteNode *)background {
SKSpriteNode *background;
NSArray *backgroundIpad;

    background = [SKSpriteNode spriteNodeWithImageNamed:@"dodgR - main background"];
    background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    background.size = CGSizeMake(1136, 640);

    NSArray *backgroundIphone = @[[SKTexture textureWithImageNamed:@"dodgR - animation 1.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 2.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 3.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 4.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 5.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 6.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 7.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 8.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 9.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 10.jpg"]];

    SKAction *backgroundIphoneAnimation = [SKAction animateWithTextures:backgroundIphone timePerFrame:0.05];

    SKAction *backgroundIphoneRepeat = [SKAction repeatActionForever:backgroundIphoneAnimation];

    [background runAction:backgroundIphoneRepeat];






background.name = @"background";
return background;

}

The name of my texture atlas is sprites.atlas Any help will be much appreciated, thanks


Solution

  • Your code above only works when using images not in a texture atlas because it doesn't actually make use of a texture atlas.

    Images can't be pulled out of an Atlas (that i know of) without using SKTextureAtlas. So you can't grab those images directly.

    SWIFT:

    let atlas = SKTextureAtlas(named: "BackgroundImages") // atlas name
    var backgroundFrames = [SKTexture]()
    
    let imageCount = atlas.textureNames.count
    for var i=1; i<= imageCount/2; i++ {
      let textureName = "dodgR - animation \(i)"
      backgroundFrames.append(atlas.textureNamed(textureName))
    }
    

    OBJECTIVE-C (not tested)

    SKTextureAtlas *atlas = [SKTextureAtlas named:@"BackgroundImages"]; // atlas name
    NSMutableArray *backgroundFrames = [NSMutableArray new];
    
    int imageCount = atlas.textureNames.count;
    
    for (int i = 1; i <= imageCount/2; i++) {
       NSString *textureName = @"dodgR - animation \(i)";
       [bacgroundFrames addObject:[atlas textureNamed:textureName]];
    }
    

    BONUS: Xcode 7 now allows you to create your sprite atlases right inside the assets folder.