baptzmoffire
baptzmoffire

Reputation: 589

How do I make an animation 2 wait for animation 1 to complete before executing, in Cocos2D, Objective-C

In my program, when you tap the screen in the iOS Sim, cards are dealt out, using CCMoveTo. The animation takes .4 seconds to complete. Tap the screen again and the next set of cards is dealt. This second animation takes .4 seconds also. All I want to do is prohibit my program from dealing out that second set of cards before the first animation is complete. Tried sleep() and, obviously, couldn't achieve the desired effect. Also tried Cocos2D's CCDelayTime a few different ways and couldn't produce the desired result. What's the easiest and/or most memory-efficient way to go about doing this?

EDIT: @crackity_jones - Here's my HelloWorldLayer file and the category I created for CCSprite to allow movement:

HelloWorldLayer.h

#import <GameKit/GameKit.h>
#import "CCSprite+MoveActions.h"
#import "cocos2d.h"

    // globals
CCSprite *card1;
...
CCSprite *card9;

@interface HelloWorldLayer : CCLayer {
    NSMutableArray *deck;
    ...
    int touchCount;
}

...

@end

HelloWorldLayer.m

...

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    touchCount++;
    [self nextRound];
}

// one method to handle all rounds dealt
-(void)nextRound {
    if (touchCount == 1) {
        [self removeAllCards];
        [deck shuffle];
        [self dealFirstRound];
    } else if (touchCount == 2) {
        [self dealSecondRound];
    } else if (touchCount == 3) {
        [self dealThirdRound];
    } else if (touchCount == 4) {
        [self dealFourthRound];
    touchCount = 0;
    }
}

-(NSMutableArray *)makeDeck {
    // create mutable array of 52 card sprites
}

-(void)dealFirstRound {
    ...
    // declare, define, add card objects to layer at start-point here
    …
    // the "moveToPositionX" methods contain the animation code
    [firstCard moveToPosition1];
    [secondCard …2];
    [third...3];
    [fourth...4];
    }

// Also methods for removing all rounds of cards
…
@end

CCSprite (MoveActions)

#import "CCSprite+MoveActions.h"
#define kCardTravelTime .1

@implementation CCSprite (MoveActions)

-(void)moveToPosition1 {
    CGSize size = [[CCDirector sharedDirector] winSize];
    [self runAction:[CCMoveTo actionWithDuration:kCardTravelTime
                                        position:CGPointMake(size.width/2 - card1.contentSize.width/4, card1.contentSize.height/2 + holeCard1.contentSize.height/5)]];
    CCDelayTime *waitTime = [CCDelayTime actionWithDuration:.4];
    [self runAction:waitTime];
}

-(void)moveToPosition2 {
    CGSize size = [[CCDirector sharedDirector] winSize];
    CCDelayTime *delay = [CCDelayTime actionWithDuration:.2];
    CCMoveTo *move = [CCMoveTo actionWithDuration:kCardTravelTime
                                        position:CGPointMake(size.width/2 + card1.contentSize.width/4, card1.contentSize.height/2 + holeCard1.contentSize.height/5)];
    [self runAction:[CCSequence actions:delay, move, nil]];
}
// rest of the methods 3-9 look like the above, essentially

@end

Upvotes: 0

Views: 1855

Answers (2)

huyleit
huyleit

Reputation: 175

You can try this! Use can insert some CCDelaytime action in CCSequence action if you need.

-(void)dealFirstRound {
    id card1Move = [CCCallBlock actionWithBlock:^{
        [firstCard moveToPosition1];
    }];
    id card2Move = [CCCallBlock actionWithBlock:^{
        [secondCard moveToPosition2];
    }];
    id card3Move = [CCCallBlock actionWithBlock:^{
        [thirdCard moveToPosition3];
    }];
    id card4Move = [CCCallBlock actionWithBlock:^{
        [fourthCard moveToPosition4];
    }];
    [self runAction:[CCSequence actions:
                     card1Move,
                     card2Move,
                     card3Move,
                     card4Move,
                     nil];
}

Upvotes: 1

skytz
skytz

Reputation: 2201

do a CCSequence like this:

[<<CCNode>> runAction:[[CCSequence actions:[deal hand],[CCDelayTime actionWithDuration:.4],[deal 2nd hand].. ,nil]];

this will do the 1st action, delay for .4 s , do the 2nd action..and so on..you can add how many actions you want

Upvotes: 0

Related Questions