2011-02-16 72 views
5

我想添加幾個標籤,每個之間有一個時間延遲順序出現。標籤將顯示0或1,並且該值是隨機計算的。我運行下面的代碼:插入時間延遲與cocos2d

for (int i = 0; i < 6; i++) { 

     NSString *cowryString; 
     int prob = arc4random()%10; 

     if (prob > 4) { 
      count++; 
      cowryString = @"1"; 
     } 
     else { 

      cowryString = @"0"; 
     } 


     [self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:0.2] ,[CCCallFuncND actionWithTarget:self selector:@selector(cowryAppearWithString:data:) data:cowryString], nil]]; 

    } 

,使標籤顯示的方法是這樣的:

-(void)cowryAppearWithString:(id)sender data:(NSString *)string { 

CCLabelTTF *clabel = [CCLabelTTF labelWithString:string fontName:@"arial" fontSize:70]; 
CGSize screenSize = [[CCDirector sharedDirector] winSize]; 
clabel.position = ccp(200.0+([cowries count]*50),screenSize.height/2); 
id fadeIn = [CCFadeIn actionWithDuration:0.5]; 
[clabel runAction:fadeIn]; 
[cowries addObject:clabel]; 
[self addChild:clabel]; 
} 

這段代碼的問題是,所有的標籤出現在同一時刻以相同的延遲。我明白,如果我使用[CCDelayTime actionWithDuration:0.2*i]代碼將工作。但問題是,我可能還需要迭代整個循環,並在第一次出現標籤後再次出現標籤。怎麼可能有行動出現延遲和行動不總是遵循相同的順序或迭代?

+0

什麼框架是在導入這個? – luca590 2011-05-24 21:10:28

回答

14

也許我真的不明白你想做什麼。但是,如果你需要一些控制當你的標籤顯示(迭代的東西)做這樣的事情:

-(void) callback 
{ 
    static int counter = 0; 
    //create your label and label action here 
    // iterate through your labels if required 
    counter++; 

    if (counter < 6) 
    { 
     double time = 0.2; 
     id delay = [CCDelayTime actionWithDuration: time]; 
     id callbackAction = [CCCallFunc actionWithTarget: self selector: @selector(callback)]; 
     id sequence = [CCSequence actions: delay, callbackAction, nil]; 
     [self runAction: sequence]; 
    } 
    else 
    { 
    //calculate the result and run callback again if required 
    //don't forget to write counter = 0; if you want to make a new throw 
    } 

} 
+0

這解決了我的問題的一部分,謝謝!請檢查下面的描述 – KDaker 2011-02-17 12:48:18

2

的問題是,你正在安排所有的動作在同一時間來火了。

更改

 [self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:0.2] ,[CCCallFuncND actionWithTarget:self selector:@selector(cowryAppearWithString:data:) data:cowryString], nil]]; 

 [self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:0.2 * i] ,[CCCallFuncND actionWithTarget:self selector:@selector(cowryAppearWithString:data:) data:cowryString], nil]]; 

應該解決您的問題