.png animation slow performance in spritekit

萝らか妹 提交于 2019-12-07 20:18:25

问题


i have a .png animation in my project with 700 pics and a size of 150 px x 150 px.

it works fine but everytime, the animation starts, the whole game freezes for about 0.1 sec. like its loading but i implemented the .png array in initWithSize. like this:

SKTextureAtlas *barrierUfoAtlas = [SKTextureAtlas atlasNamed:@"BarrierUfoAnimation"];
NSArray *barrierUfoAtlasNames = [barrierUfoAtlas textureNames];
NSArray *sortetBarrierUfoAtlasNames = [barrierUfoAtlasNames sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSMutableArray *barrierUfoTextures = [NSMutableArray array];

for (NSString *filename in sortetBarrierUfoAtlasNames) {
    SKTexture *texture = [barrierUfoAtlas textureNamed:filename];
    [barrierUfoTextures addObject:texture];
}
self.barrierUfoAnimation = [SKAction animateWithTextures:barrierUfoTextures timePerFrame:0.024];

and then while playing after about 1-2 min. the animation is starting. it doesn´t need to load anything at this point, just start the animation. is there any way to improve it?


回答1:


This is one of many ways to preload:

@implementation GameScene {
SKTextureAtlas *myAtlas1;
BOOL loadingComplete;
}

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {

    // the usual stuff...

    loadingComplete = false;
    [self loadMyAtlas1];
    }   

    return self;
}

-(void)loadMyAtlas1 {
    myAtlas1 = [SKTextureAtlas atlasNamed:@"MyAtlasName"];
    [SKTextureAtlas preloadTextureAtlases:[NSArray arrayWithObject:myAtlas1] withCompletionHandler:^{
    [self finishedLoading];
    }];
}

-(void)finishedLoading {
    // other stuff you might do here
    loadingComplete = true;
}

-(void)update:(CFTimeInterval)currentTime {
    if(loadingComplete) {
        // run game code
    } else {
        // wait for the water to boil
    }
}


来源:https://stackoverflow.com/questions/28056269/png-animation-slow-performance-in-spritekit

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