Edward Glasser
Edward Glasser

Reputation: 193

cocos2d iphone wait for action to complete/finish

I'm trying to force an action to complete before another action can be called. I've looked all over for different examples of timing delays and CCSequencing and this is what i have so far, but it still isn't working. I need the full .5 to come out before another action (whether it be left, right or down) can be called. Everything is working as long as you don't hit the buttons before an action finishes, thats when the previous action is cut short.

-(void) moveup{
    CCNode *mySprite = [self getChildByTag:kTagSprite];
    //moves sprite up 30 pixels in 1/2 second
    [mySprite runAction: [CCMoveTo actionWithDuration:.5 position:ccp(mySprite.position.x, mySprite.position.y+30)]];
}

//this is inside CCtouchesbegan.  upsprite is a box that gets clicked to move the object up
if(CGRectContainsPoint([upsprite boundingBox], point)){
    //should call CCSequence of events, method moveup, and then CCDelayTime, forcing a pause
    [self runAction:[CCSequence actions:[CCCallFuncND actionWithTarget:self selector:@selector(moveup) data:nil], [CCDelayTime actionWithDuration:.5], nil]];
}

I've tried pausing using [[CCDirector sharedDirector] pause] but this freezes everything, including the sprite I'm moving. The CCDelayTime doesn't seem to be working in my current example.

I know this question is kind of a duplicate of a couple others about ccsequencing, so if it needs to be marked as a dupe, that's fine, but I'd really like some help as it's really holding me back.

Upvotes: 0

Views: 3438

Answers (1)

Alexander Blunck
Alexander Blunck

Reputation: 1223

You could make a BOOL "lock" variable, and only run the action on "mySprite" :

if(lock) {[mySprite runAction:...];}

set the BOOL lock to NO in the beginning and everytime you begin the action set it to "YES"...then add a CCCallFunc to the CCSequence to set the lock back to "NO"

Upvotes: 1

Related Questions