我正在使用 CCScrollLayer,我想在玩家选择阶段期间准备图像。这很简单,但是当我滚动阶段时,它会处理时间(延迟加载图像)。
所以我决定使用 NSThread。我从 cocos2d 收到一条消息“cocos2d: CCSpriteFrameCache: Trying to use file 'Level3.png' as texture”。那么它应该会出现。但是我在线程上加载的这些图像并没有像我想要的那样出现。根本不值一提。
-(void) moveToStagePage:(int)page
{
...
[NSThread detachNewThreadSelector:@selector(prepareTexture:) toTarget:self withObject:[NSNumber numberWithInt:page]];
...
}
下面的源代码是准备图像的代码。(线程)
-(void) prepareTexture:(NSNumber*)number
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int _page = [number intValue];
NSLog(@"%d Thread start", _page);
if(loadingTexNum != 0 && (_page + 1) != loadingTexNum)
{
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromFile:[NSString stringWithFormat:@"Level%d.plist", loadingTexNum]];
loadingTexNum = _page + 1;
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:[NSString stringWithFormat:@"Level%d.plist", loadingTexNum]];
}
if(loadingTexNum == 0 && (_page + 1) != loadingTexNum)
{
loadingTexNum = _page + 1;
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:[NSString stringWithFormat:@"Level%d.plist", loadingTexNum]];
}
[NSThread sleepForTimeInterval:10.0];
NSLog(@"%d Thread release", _page);
[pool release];
}