2011-02-16 74 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]]; 

应该解决您的问题