2

how in order load music, images, and other stuff to scene with progress bar and move bar smooth.. i guess logic of progress bar is to create new thread - load data and destroy thread here is my code to load stuff but it's not work, progress bar appears but not updating value

-(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;
}

Thanks in advance.

interface.. there you go..)

@interface LoadScene : CCScene
{
    GPLoadingBar *loadingBar;
    float value;
}
user1644430
  • 90
  • 2
  • 9

1 Answers1

3

Ok, so you should load resources in background, while updating loadBar in main thread. For the SimpleAudioEngine you really need NSThread, but CCTextureCashe has -addImageAsync method which allow you to load images asynchronously without problems. So your code should look something like this:

-(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 has its own updating loop, so you don't need to create your one for updating loadingBar. It also has a drawing loop, so if you want to dynamically update something on screen you should only set up values for update and not to stop main thread with loading resources

also, you could use [self s1] instead of [self performSelector:@selector(s1)] because they are identical. Hope that would help

medvedNick
  • 4,512
  • 4
  • 32
  • 50