私のゲームでは、ゲームプレイ中にシーンの背景を変更する必要があります。背景に新しいテクスチャを設定すると、ゲームの速度が少し遅くなります。これを回避するために、テクスチャを非同期でプリロードしてから、メインスレッドに表示しようとしています。これは私がそれを行う方法です:
NSString *filename = [NSString stringWithFormat:@"res/src/level_%i/background.png", [GameLevel sharedGameLevel].currentLevelIndex + 1];
__block CCTexture2D *texture;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"FILENAME %@", filename);
[[CCTextureCache sharedTextureCache] addImage:filename];
NSLog(@"%@", [CCTextureCache sharedTextureCache]);
dispatch_async(dispatch_get_main_queue(), ^{
texture = [[CCTextureCache sharedTextureCache] textureForKey:filename];
[spareBackground setTexture:texture];
[dayBackground runAction:[CCSequence actions:fadeOut,[CCCallBlockN actionWithBlock:^(CCNode *node)
{
NSLog(@" TEXTURE %@", texture);
[dayBackground setTexture:texture];
CCFadeIn *fadeIn = [[[CCFadeIn alloc] initWithDuration:5] autorelease];
[dayBackground runAction:fadeIn];
}], nil]];
});
});
しかし、背景の代わりに、テクスチャが正常にロードされたにもかかわらず、常に空白の画面が表示されますが、そうではありませんnil
。このコードは、gcdを使用せずにテクスチャがメインスレッドにロードされている場合は問題なく機能します。私は何が欠けていますか?