Seany242
Seany242

Reputation: 373

Using CCDelay Time in a loop

I have 6 menu items on my screen that I want to slide off the screen one by one after the help button (one of the 6 items) is clicked. Here's my code:

NSArray * menuArray = [NSArray arrayWithObjects:item1, item2, item3, item4, item6, item5, nil];
CCDelayTime * delayM = [CCDelayTime actionWithDuration:1.4];
for (CCMenuItem * item in menuArray) {
    id moveLeft = [CCMoveBy actionWithDuration:0.7 position:ccp(10, 0)];
    id moveRight = [CCMoveBy actionWithDuration:0.4 position:ccp(-200, 0)];
    //CCDelayTime * delayM = [[CCDelayTime alloc] initWithDuration:1.4];
    [item runAction:[CCSequence actions:moveLeft, moveRight, delayM, nil]];
}

For some reason, the delay doesn't seem to make any difference here and all of the menu items slide off the screen at the same time. How can I make it so that the menu items won't slide off the screen until the last one already has?

Sidenote: I can't figure out how to cancel all of the selectors that these menu items are hooked up to so that the user won't accidentally touch a selector while this whole animation is going on. Could someone help me with this as well?

Upvotes: 1

Views: 919

Answers (1)

CrimsonDiego
CrimsonDiego

Reputation: 3616

This is because they all start those actions at the same time.

This is how you might do it:

NSArray * menuArray = [NSArray arrayWithObjects:item1, item2, item3, item4, item6, item5, nil];
float delay = 0;
for (CCMenuItem * item in menuArray) {
    CCDelayTime * delayM = [CCDelayTime actionWithDuration:delay];
    id moveLeft = [CCMoveBy actionWithDuration:0.7 position:ccp(10, 0)];
    id moveRight = [CCMoveBy actionWithDuration:0.4 position:ccp(-200, 0)];
    [item runAction:[CCSequence actions:delayM,moveLeft, moveRight,  nil]];
    delay += 1.1;
}

Remember, the runAction is asynchronous - so to make them do it one by one, you have to start with no delay, and for each item, add to the delay the length of time it will take for the previous item's actions. In this case, every item's actions take 1.1 seconds (.7 for the move left, and .4 for the move right), so we extend the delay-before-movement by 1.1 seconds every item.

Upvotes: 1

Related Questions