我想在我的应用程序中实现启动加载器。应该是这样的:启动闪屏后,用户会看到简单的动画,同时app会预加载音效、背景音乐、精灵图片、精灵表等。当前实施:
- (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还可以