views:

15

answers:

2

I have a loading screen that I initialise with a label to display the loading progression. I want to call my DataManager AFTER initialising the loading screen, and then call a method to switch scenes. Here is my code:

-(id) init {
  if((self=[super init]))
  {
    loadingLabel = ....
    [self addChild:loadingLabel];

    /***** This is what I want to call after the init method
    //DataManager loads everything needed for the level, and has a reference to the 
    //loading screen so it can update the label
    [[DataManager sharedDataManager] loadLevel:@"level1" screen:self];
    //this will switch the scene
    [self finishLoading];
    *****/
  }
  return self;
}
-(void) setLoadingPercentage:(int) perc {
   //changes the label
}
-(void) finishLoading {
    [[CCDirector sharedDirector] replaceScene:[Game node]];
}

So I can't call the datamanager in the init, because the label will not get updated as content gets loaded, and I can't switch scenes in the init method. So How do I run my datamanger and finish loading AFTER the init? My plan was to set a schedule at an interval of 1 second that does this, but it doesn't seem right to have to wait a second.

EDIT: another way I could do it is to schedule at every frame and ask the datamanager where its at... This seems better already, since the datamanager wouldn't need a reference to the loading screen.

Any ideas?

+1  A: 

You can use performSelector:withObject:afterDelay: to force the execution of the specified selector during the next iteration of the current threads run loop:

[self performSelector:@selector(finishLoading) withObject:nil afterDelay:0.0f];
Jacob Relkin
A: 

The above answer is correct, but you should use the Cocos2d way to schedule methods to run later:

[self schedule:@selector(finishLoading) interval:0.1];

-(void)finishLoading
{
  [self unschedule:@selector(finishLoading)];
  //Do your stuff
}
pabloruiz55