我试图找出这两个示例与preloadTextureAtlases :withCompletionHandler的工作方式之间的区别。以下是代码:
//GameScene.m
-(void)didMoveToView:(SKView *)view {
//First I create an animation, just a node moving from one place to another and backward.
//Then I try to preload two big atlases
[SKTextureAtlas preloadTextureAtlases:@[self.atlasA, self.atlasB] withCompletionHandler:^{
[self setupScene:self.view];
}];我认为preloadTextureAtlases在后台线程上加载是因为我的动画很流畅吗?
但是,从后台线程调用preloadTextureAtlases有什么不同(或者有问题)吗?如下所示:
//GameScene.m
- (void)loadSceneAssetsWithCompletionHandler:(CompletitionHandler)handler {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[SKTextureAtlas preloadTextureAtlases:@[self.atlasA, self.atlasB] withCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self setupScene:self.view];
});
}];
if (!handler){return;}
dispatch_async(dispatch_get_main_queue(), ^{
handler();
});
});
}然后从didMoveToView调用此方法:
[self loadSceneAssetsWithCompletionHandler:^{
NSLog(@"Scene loaded");
// Remove loading animation and stuff
}];发布于 2015-03-31 13:28:44
你可以在后台预加载,但问题是你为什么要呢?机会是你的游戏不能开始玩,直到所有所需的资产被加载,所以用户将不得不等待,不管。
您可以预加载任何您需要的资产,让游戏开始和背景加载任何额外的资产,您可能需要在以后的游戏发挥。只有在非常复杂的游戏中才需要这样的环境,而不是在iOS平台上。
至于你的问题加载在后台,而游戏正在进行,没有明确的答案。这完全取决于负载的多大,你的CPU负载等等.试一试,看看会发生什么,因为听起来你在问一些你还没有试过的问题。
如果您真的打算执行后台任务,请记住,您可以向自定义方法添加一个完成块,如下所示:
-(void)didMoveToView:(SKView *)view {
NSLog(@"start");
[self yourMethodWithCompletionHandler:^{
NSLog(@"task done");
}];
NSLog(@"end");
}
-(void)yourMethodWithCompletionHandler:(void (^)(void))done; {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// a task running another thread
for (int i=0; i<100; i++) {
NSLog(@"working");
}
dispatch_async(dispatch_get_main_queue(), ^{
if (done) {
done();
}
});
});
}https://stackoverflow.com/questions/29358003
复制相似问题