Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inserting time delay with cocos2d

I am trying to add several labels that appear sequentially with a time delay between each. The labels will display either 0 or 1 and the value is calculated randomly. I am running the following code:

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

    }

the method that makes the labels appear is this:

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

The problem with this code is that all the labels appear at the same moment with the same delay. I understand that if i use [CCDelayTime actionWithDuration:0.2*i] the code will work. But the problem is that i might also need to iterate this entire for loop and have the labels appear again after they have appeared the first time. how is it possible to have actions appear with delay and the actions dont always follow the same order or iterations???

like image 249
KDaker Avatar asked Feb 16 '11 18:02

KDaker


2 Answers

Maybe i did not really understand what you want to do. But if you need some control when your labels appear (to iterate something) make something like this:

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

}
like image 103
Andrew Avatar answered Nov 04 '22 18:11

Andrew


The problem is that your are scheduling all the actions to fire off at the same time.

Changing

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

for

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

should fix your problem

like image 33
Sergi Avatar answered Nov 04 '22 17:11

Sergi