Sprite Kit animateWithTextures lags

ぃ、小莉子 提交于 2019-12-19 04:22:37

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/28041605/sprite-kit-animatewithtextures-lags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!