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;
    }];
}
有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top