Question

I have a few frames of animations on my node, and the first time I play the animation it lags, fps drops. Each next time is fine and dandy.

How do I preload the textures to make it work smooth?

I have this method to run when I load the game:

- (void)load
{
    self.animationFrames = @[[SKTexture textureWithImageNamed:@"exp1"], [SKTexture textureWithImageNamed:@"exp2"],
                             [SKTexture textureWithImageNamed:@"exp3"], [SKTexture textureWithImageNamed:@"exp4"], [SKTexture textureWithImageNamed:@"exp5"], [SKTexture textureWithImageNamed:@"exp6"], [SKTexture textureWithImageNamed:@"exp7"], [SKTexture textureWithImageNamed:@"exp8"], [SKTexture textureWithImageNamed:@"exp9"]];
}

And this method to play animation:

-(void)playExplosionAnimation
{
    self.size = CGSizeMake(250, 250);
    SKAction *animation = [SKAction animateWithTextures:self.animationFrames timePerFrame:0.1];

    [self runAction:animation completion:^{
        self.hidden = YES;
    }];
}
Was it helpful?

Solution

You should create a texture atlas and use SKTextureAtlas methods:

– preloadWithCompletionHandler:
+ preloadTextureAtlases:withCompletionHandler:

Here is what documentation says about this:

Sprite Kit creates a background task that loads the texture data from the atlas. Then, Sprite Kit returns control to your game. After the texture atlas is loaded, your completion handler is called.

If you need to preload multiple texture atlases at once, use the preloadTextureAtlases:withCompletionHandler: method instead.

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