I'm using texture atlases in my Sprite Kit game. I'm creating SKTextureAtlas
object and store it's textures in array for each animation. So when I need some animation on my hero I call animateWithTextures
sending it the corresponding array. There are some lags when I start animations. Is there some way to start animation smoothly?

- 469
- 7
- 19
-
1Read the section "Simplifying SpriteKit's animation handling". https://www.codeandweb.com/blog/2013/09/23/spritekit-animations-and-textureatlases – sangony Jan 20 '15 at 13:42
2 Answers
I am sure there are few ways to get around this. What you need to do is to preload an atlases before your gameplay actually start. Just show a loading screen at the beginning of the game and preload your atlases.
You may try with + preloadTextureAtlases:withCompletionHandler:
[SKTextureAtlas preloadTextureAtlases:textureAtlasesArray withCompletionHandler:^{ /*Game Start*/}];
Another way to implement resource loading before everything else (and keep everything in memory) is described here in Adventure game example
For more details about loading assets asynchronously take a peek into code which can be downloaded from the link above.

- 14,286
- 11
- 68
- 157
-
I think this is exactly what I'm doing, I preload textures before game appears and keep them in memory (in arrays as I said). But there are still lags after I call animateWithTextures. Maybe texture atlases are too big? – tagirkaZ Jan 20 '15 at 13:09
-
1You have to balance the structure of your atlases and choose between what gives you the best performance. If you choose to store an atlases with too few images, SpriteKit will need probably a lot of draw passes to render a frame. If you decide to use too many images in a single atlas, then a lot of memory will be needed for loading...How many draw passes do you have? Do you testing on a device or simulator ? – Whirlwind Jan 20 '15 at 13:32
-
I'm testing on a device. I have several atlases. The smallest atlas has 3 draw passes and the biggest one has 46. – tagirkaZ Jan 20 '15 at 14:08
-
1Sorry, I think we are not talking about the same thing. What are your draw count and node count in your gameplay? You can enable these options with SKView.showsDrawCount and showsNodeCount. Also, you can optimize your animation like @sangony pointed, or what would probably help is to reduce the number of images from animation and increase the value of timePerFrame parameter. – Whirlwind Jan 20 '15 at 14:46
-
My draw count is 33 average and node count is 35 average. I've read the link that @sangony suggested but I didn't understand what exactly I should do. I've tried to create a property to store SKTextureAtlas but it didn't change anything. I store all my textures in arrays so what benefits assigning atlas to my property can give to me? – tagirkaZ Jan 22 '15 at 10:33
-
133 draws seems too much for 35 nodes and this can affect on overall performance. You can find useful to read about stuff like Ignore siblings order, Blending mode and Drawing order. You probably use many atlases or your nodes are organized in such a way that SpriteKit can't draw multiple nodes in a single pass. Also I can't say this is the cause of the animateWithTextures lag especially without being able to look into code and the atlases structure. I can provide you with some useful links about performance tips if you like... – Whirlwind Jan 22 '15 at 13:40
-
Understanding the Drawing Order for a Node Tree and ignoreSiblingsOrder property : https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Nodes/Nodes.html#//apple_ref/doc/uid/TP40013043-CH3-SW6 – Whirlwind Jan 22 '15 at 17:32
-
This is a nice one about texture optimization : http://www.learn-cocos2d.com/2012/11/optimize-memory-usage-bundle-size-cocos2d-app/ – Whirlwind Jan 22 '15 at 17:43
-
A little from everything - Performance tips and tricks in iOS programming : http://www.raywenderlich.com/31166/25-ios-app-performance-tips-tricks I hope this would be of some help. Happy coding! :) – Whirlwind Jan 22 '15 at 17:51
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69442/discussion-between-tagirkaz-and-whirlwind). – tagirkaZ Jan 23 '15 at 11:59
had the same problem and I solved it in my game by not using atlases. So try this example:
-(void)makePlayerAnimation:(SKSpriteNode *)player
{
SKTexture *texture1 = [SKTexture textureWithImageNamed:@"texture1.png"];
SKTexture *texture2 = [SKTexture textureWithImageNamed:@"texture2.png"];
SKTexture *texture3 = [SKTexture textureWithImageNamed:@"texture3.png"];
SKAction *animationTextures = [SKAction animateWithTextures:@[texture1, texture2, texture3] timePerFrame:0.1];
[player runAction:animationTextures];
}
When you wish to activate animation do this:
[self makePlayerAnimation:myNode];
or
[self makePlayerAnimation:self.myNode];
Just depends how you declared it. If you need to run animation forever, you can just add line at the end of previous method:
SKAction *repeat = [SKAction repeatActionForever: animationTextures];
Hope this helps.

- 97
- 2
- 10