2011-02-16 15 views
5

Estoy tratando de agregar varias etiquetas que aparecen secuencialmente con un retraso de tiempo entre cada una. Las etiquetas mostrarán 0 o 1 y el valor se calcula aleatoriamente. Estoy funcionando con el siguiente código:insertando el retraso de tiempo con 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]]; 

    } 

el método que hace que las etiquetas aparecen es el siguiente:

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

El problema con este código es que todas las etiquetas aparecen en el mismo momento con el mismo retardo . Entiendo que si uso [CCDelayTime actionWithDuration:0.2*i] el código funcionará. Pero el problema es que también podría necesitar iterar todo este ciclo for y hacer que las etiquetas aparezcan nuevamente después de que aparecieron la primera vez. ¿Cómo es posible que las acciones aparezcan con retraso y las acciones no siempre siguen el mismo orden o las mismas iteraciones?

+0

qué marco estás importando para esto? – luca590

Respuesta

14

Tal vez no entendí realmente lo que quiere hacer. Pero si necesita algún tipo de control cuando aparecen las etiquetas (para repetir algo) hacer algo como esto:

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

esto resuelve parte de mi problema gracias! por favor, consulte la descripción a continuación – KDaker

+0

@KDaker: he editado la respuesta – Andrew

+0

gracias he visto ... funciona muy bien! – KDaker

2

El problema es que su programando todas las acciones de disparar al mismo tiempo.

Cambio

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

para

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

debería solucionar su problema

Cuestiones relacionadas