Reputation: 13675
What I am trying to do is to run one action (playForwardAnimation) which is consisting of multiple actions (running on different nodes) and after all actions are done, to run an action called playBackwardAnimation. Here is the code: `
SKAction *wait = [SKAction waitForDuration:3.5];
SKAction *forwardAnimationAction = [SKAction animateWithTextures:forwardAnimationFrames timePerFrame:0.1];
SKAction *backwardAnimationAction = [SKAction animateWithTextures:backwardAnimationFrames timePerFrame:0.1];
SKAction *forwardAnimation = [SKAction sequence:@[wait , forwardAnimationAction, wait, forwardAnimationAction]];
SKAction *backwardAnimation = [SKAction sequence:@[wait , backwardAnimationAction, wait, forwardAnimationAction]];
SKAction *playForwardAnimation = [SKAction runBlock:^{
[node5 runAction:forwardAnimation completion:^{
[node4 runAction:[SKAction rotateByAngle:-0.1 duration:0.3]];
[node3 runAction: [SKAction rotateByAngle:-0.2 duration:0.3]];
[node2 runAction: [SKAction rotateByAngle:-0.3 duration:0.3]];
NSLog(@"forward action executed");
}];
}];
SKAction *playBackwardAnimation = [SKAction runBlock:^{
[node5 runAction:backwardAnimation completion:^{
[node4 runAction:[SKAction rotateByAngle:0.1 duration:0.3]];
[node3 runAction: [SKAction rotateByAngle:0.2 duration:0.3]];
[node2 runAction: [SKAction rotateByAngle:0.3 duration:0.3]];
NSLog(@"backward action executed");
}];
}];
SKAction *sequence = [SKAction sequence:@[playForwardAnimation,playBackwardAnimation]];
SKAction *action = [SKAction repeatActionForever:sequence];
[holder runAction:action];`
The problem is that playBackwardAnimation is not called after the playForwardAnimation is over completely. It looks like both animations are called at the same time. How this can be done? The hierarchy of nodes used in animation looks like this:
holder (upperArea)
upperArea (node4,node3,node2)
node4(node5)
Upvotes: 2
Views: 436
Reputation: 8130
Unfortunately it gets messy when youre running actions on different nodes like this and you need a strict sequence. Here's one way to do it. I'm sure there are many.
Hopefully I got this right
SKAction *wait = [SKAction waitForDuration:3.5];
SKAction *forwardAnimationAction = [SKAction animateWithTextures:forwardAnimationFrames timePerFrame:0.1];
SKAction *backwardAnimationAction = [SKAction animateWithTextures:backwardAnimationFrames timePerFrame:0.1];
SKAction *forwardAnimation = [SKAction sequence:@[wait , forwardAnimationAction, wait, forwardAnimationAction]];
SKAction *backwardAnimation = [SKAction sequence:@[wait , backwardAnimationAction, wait, forwardAnimationAction]];
CGFloat rotateDur = 0.3;
SKAction *rotateWait = [SKAction waitForDuration:rotateDur];
SKAction *forwardAnimationBlock = [SKAction runBlock:^{
[node4 runAction:[SKAction rotateByAngle:-0.1 duration:rotateDur]];
[node3 runAction: [SKAction rotateByAngle:-0.2 duration:rotateDur]];
[node2 runAction: [SKAction rotateByAngle:-0.3 duration:rotateDur]];
}];
SKAction *backwardAnimationBlock = [SKAction runBlock:^{
[node4 runAction:[SKAction rotateByAngle:-0.1 duration:rotateDur]];
[node3 runAction: [SKAction rotateByAngle:-0.2 duration:rotateDur]];
[node2 runAction: [SKAction rotateByAngle:-0.3 duration:rotateDur]];
}];
SKAction *sequence = [SKAction sequence:@[forwardAnimation, forwardAnimationBlock, rotateWait, backwardAnimation, backwardAnimationBlock, rotateWait]];
Upvotes: 3