我想在我的应用中实现启动加载器。它应该是这样的:启动启动画面后,用户将观看简单的动画,同时应用预装声音效果,背景音乐,精灵图像,精灵表等。目前的实施:
- (id)init {
if((self = [super init])) {
// Some other setup ...
CGRect rect;
rect = waveSprite.textureRect;
waveInitialTexRectOrigin = rect.origin;
rect.size.width = 91;
waveSprite.textureRect = rect;
assetFilenames = [[NSArray alloc] initWithObjects:
// images
@"background.png",
// spritesheets
@"sprites.plist",
// fonts
@"main.png",
// sound effects
@"button.wav",
nil];
assetCounter = 0;
[self loadAsset];
}
return self;
}
- (void)update:(ccTime)dt {
CGRect rect;
rect = waveSprite.textureRect;
rect.origin.x += dt*kLoaderWaveSpeed;
while (rect.origin.x > waveInitialTexRectOrigin.x + kLoaderWavePeriod) {
rect.origin.x -= kLoaderWavePeriod;
}
waveSprite.textureRect = rect;
}
#pragma mark Private
- (void)loadAsset {
// CCLOG(@"loadAsset");
NSString *filename = [assetFilenames objectAtIndex:assetCounter];
CCLOG(@"loading %@", filename);
NSString *ext = [filename pathExtension];
if ([ext doesMatchRegStringExp:@"[png|jpg]"]) {
[[CCTextureCache sharedTextureCache] addImage:filename];
} else if ([ext isEqualToString:@"plist"]) {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:filename];
} else if ([ext doesMatchRegStringExp:@"[caf|wav|mp3]"]) {
[[SimpleAudioEngine sharedEngine] preloadEffect:filename];
}
assetCounter++;
if (assetCounter < [assetFilenames count]) {
[self performSelector:@selector(loadAsset) withObject:self afterDelay:0.1f];
} else {
[self performSelector:@selector(loadingComplete) withObject:self afterDelay:0.2];
}
但动画很突然。
UPD 我已经尝试了
[self performSelectorInBackground: withObject:]
但它似乎不起作用(挂载加载第一个资产)。也许我应该朝这个方向努力。
UPD2 平滑=不突然,没有延迟和闪烁。 fps无所谓,20 fps相当不错
答案 0 :(得分:2)
我没有CC2D的经验,但这是我在ViewController中用来显示平滑动画加载指示器的内容:
//Set up and run your loading scene/indicator here
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
//Load your scene
dispatch_async( dispatch_get_main_queue(), ^{
//This fires when the loading is complete
//Show menu and destroy the loader scene/indicator here
});
});
我想这值得一试。
答案 1 :(得分:1)
您不应该再使用performSelectorInBackground,它不会被弃用,但可能很快就会出现,GCD现在已经出现了。
我会尝试在主线程中运行动画,因为它是唯一可以进行UI更新的动画。并使用GCD生成一个或多个线程来加载所有资产。
我会使用并发调度队列来并行加载所有内容,并在完成后立即关闭动画。
您可以在此处获得有关并发调度队列的完整信息:
希望有所帮助。
答案 2 :(得分:0)