2

如何将音乐、图像和其他内容加载到带有进度条的场景并平滑移动条.. 我猜进度条的逻辑是创建新线程-加载数据和销毁线程这是我加载内容的代码,但它不起作用, 进度条出现但不更新值

-(void)s1
{
    [[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"game_music.caf"];
}

-(void)s2
{
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"tap.caf"];
}

-(void)startThread
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    EAGLContext *context = [[[EAGLContext alloc]
                               initWithAPI:kEAGLRenderingAPIOpenGLES1
                                sharegroup:[[[[CCDirector sharedDirector] openGLView] context] sharegroup]] autorelease];
    [EAGLContext setCurrentContext:context];

    [self performSelector:@selector(loadBar)];
    //[self schedule:@selector(tick:)];
    [self performSelector:@selector(s1)];                   // uploading file
    [self performSelector:@selector(progressUpdateValue)];  // add 10 value to progress
    [self performSelector:@selector(s2)];                   // uploading file
    [self performSelector:@selector(progressUpdateValue)];  // add 10 value to progress

    [self performSelector:@selector(replaceScene)
                 onThread:[[CCDirector sharedDirector] runningThread]
               withObject:nil
            waitUntilDone:false];
    [pool release];

}

-(void)replaceScene
{
    [[CCDirector sharedDirector]replaceScene:[GameScene node]];
}

-(id)init
{
    self = [super init];
    if (self != nil)
    {
        [NSThread detachNewThreadSelector:@selector(startThread) toTarget:self withObject:nil];
    }
    return self;
}

提前致谢。

界面..你去..)

@interface LoadScene : CCScene
{
    GPLoadingBar *loadingBar;
    float value;
}
4

1 回答 1

3

好的,所以您应该在后台加载资源,同时在主线程中更新 loadBar。对于 SimpleAudioEngine,您确实需要 NSThread,但 CCTextureCshe 具有 -addImageAsync 方法,该方法允许您异步加载图像而不会出现问题。所以你的代码应该是这样的:

-(void)s1
{
    [[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"game_music.caf"];
}

-(void)s2
{
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"tap.caf"];
}

-(void)startThread
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [self s1];
    loadingBar.value = 0.5;
    [self s2];

    [self performSelectorOnMainThread:@selector(replaceScene)
                 withObject:nil
            waitUntilDone:false];
    [pool release];

}

-(void)replaceScene
{
    [[CCDirector sharedDirector]replaceScene:[GameScene node]];
}

-(id)init
{
    self = [super init];
    if (self != nil)
    {
        [self loadBar];
        [NSThread detachNewThreadSelector:@selector(startThread) toTarget:self withObject:nil];
    }
    return self;
}

-(void) loadingFinished
{
    [self replaceScene];
}

Cocos2d 有自己的更新循环,所以你不需要创建一个更新 loadingBar。它还有一个绘图循环,所以如果你想动态更新屏幕上的东西,你应该只设置更新值,而不是停止加载资源的主线程

此外,您可以使用 [self s1] 而不是 [self performSelector:@selector(s1)] 因为它们是相同的。希望这会有所帮助

于 2013-01-16T15:22:27.123 回答