2010-07-30 48 views
1

我有一個加載屏幕,我用一個標籤初始化來顯示加載進程。 我想在初始化加載屏幕後調用我的DataManager,然後調用切換場景的方法。這裏是我的代碼:在cocos2d初始化後運行一個方法

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

所以我不能把在init的數據管理器,因爲當內容被加載標籤不會得到更新,我不能在init方法切換場景。那麼如何運行我的數據管理器並在init之後完成加載?我的計劃是以1秒爲間隔設定一個時間表,但這似乎不適合等待一秒鐘。

編輯:另一種方式,我可以做到這一點是安排在每一幀,並要求datamanager它在...這似乎更好,因爲datamanager不需要參考加載屏幕。

任何想法?

回答

3

您可以使用performSelector:withObject:afterDelay:迫使當前線程的下一次迭代過程中指定選擇的執行運行循環:

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

以上的答案是正確的,但你應該使用cocos2d的方式來安排方法稍後運行:

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

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