0

I have this code inside of my initial view controller, in the function viewWillLayoutSubviews:

    SKTextureAtlas *obstacleAtlas = [SKTextureAtlas atlasNamed:@"gonImages"];
    obstacleAtlas = [SKTextureAtlas atlasNamed:@"gonImages"];
    SKTexture *castle = [obstacleAtlas textureNamed:@"castle@2x.png"];
    SKTexture *column = [obstacleAtlas textureNamed:@"columnNE@2x.png"];
    SKTexture *factory = [obstacleAtlas textureNamed:@"factory@2x.png"];
    SKTexture *hotAirBallon = [obstacleAtlas textureNamed:@"hotAirBallon@2x.png"];
    SKTexture *lightningBolt = [obstacleAtlas textureNamed:@"lightningBolt@2x.png"];
    SKTexture *pirateShip = [obstacleAtlas textureNamed:@"pirateShip@2x.png"];
    SKTexture *pyramidNE = [obstacleAtlas textureNamed:@"PyramidNE@2x.png"];
    SKTexture *rocket = [obstacleAtlas textureNamed:@"rocket@2x.png"];
    SKTexture *sun = [obstacleAtlas textureNamed:@"sun@2x.png"];
    SKTexture *wheel = [obstacleAtlas textureNamed:@"wheel@2x.png"];
    NSArray *obstacleTextures = @[castle, column, factory, hotAirBallon, lightningBolt, pirateShip, pyramidNE, rocket, sun, wheel];
    [SKTexture preloadTextures:obstacleTextures withCompletionHandler:^{
        // Present the scene.
        [skView presentScene:startScene];
        //[skView presentScene:scene];
    }];

And I want to load these textures before my game starts so that I do not have lag during the gameplay. However this does not appear to be working because whenever new spritenodes that are made from images found in my obstacleAtlas the game lags. Am I preloading correctly? If not, how and where should I be doing this.

Max Hudson
  • 9,961
  • 14
  • 57
  • 107

1 Answers1

1

those tectures are local variables, when the preloading method returns local variables are released by ARC. You need to keep the texture objects "alive" (ie make them ivars) for preloading to have the desired effect.

Also if these textures consume a lot of memory the unused ones may be removed from memory as more textures are being loaded. Check for any memory notifications.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • How do I go about making them global? Is this done in the app delegate as properties? – Max Hudson Feb 08 '14 at 01:32
  • i didn't say global, make them ivars of the viewcontroller – CodeSmile Feb 08 '14 at 08:34
  • So you just need to load and retain the images, but you don't need to make them directly accessible from elsewhere within the app? basically your just loading them so they are cached in the viewController, the next time you load them from your SKScene they will be loaded directly from the cache (as apposed to the disk). – fuzzygoat Feb 08 '14 at 22:52