0

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

RT33
  • 103
  • 9
  • Try to remove .jpg from image name . If that doesn't help, delete simulator's content and settings / delete an app from device, and build and run again. And, what do you consider by "not loading"? Do you see missingResource image(white image with red X) or something else happening? Also, is your background node added correctly to the scene? – Whirlwind Oct 09 '15 at 13:45
  • All nodes contain a red X. Removing .jpg did not help – RT33 Oct 09 '15 at 20:49
  • the only time the textures do appear on the screen is when they are not in an atlas – RT33 Oct 09 '15 at 20:50
  • I was not concentrated so much on your code, but rather on issues you are experiencing ... So, as Beau Young stated, you have to use atlases properly in your code in order to load images from them. – Whirlwind Oct 12 '15 at 22:53

1 Answers1

2

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.

Beau Nouvelle
  • 6,962
  • 3
  • 39
  • 54