我的项目中有一个.png动画,有700张图片,大小为150像素x 150像素。
它工作正常,但每次动画开始,整个游戏冻结约0.1秒。喜欢它的加载,但我在initWithSize中实现了.png数组。像这样:
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];
然后在约1-2分钟后播放。动画开始了。 此时不需要加载任何内容,只需启动动画即可。 有没有办法改善它?
答案 0 :(得分:4)
这是预加载的众多方法之一:
@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
}
}