Friday, 15 March 2013

iphone - Can't display a texture loaded asynchronously -



iphone - Can't display a texture loaded asynchronously -

in game have alter background of scene during gameplay time. when set new texture background game slows downwards moment. in order escape i'm trying preload texture asynchronously , show on main thread. how that:

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]]; }); });

but instead of background receive blank screen despite texture has been loaded, it's not nil. code works fine if texture loaded on main thread without using gcd. missing?

my suspect cctexturecache not thread-safe (and beingness shared object need thread safe in order safely called thread).

cocos2d, on other hand provides mechanism load texture asynchronously, might utilize them instead. should signature:

[[cctexturecache sharedtexturecache] addimageasync:filename target:self selector:@selector(textureloaded:)];

iphone ios objective-c cocos2d-iphone grand-central-dispatch

No comments:

Post a Comment