Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top