5

I know that I can create an SKAction.sequence which will run the actions of one node one by one. But how can I do that if I want to do a sequence with different nodes. I'd like to do something like that:

  1. Run action from node A
  2. wait 2 seconds
  3. Run action from node B.
godel9
  • 7,340
  • 1
  • 33
  • 53
Christian
  • 22,585
  • 9
  • 80
  • 106

1 Answers1

8

If both nodes are uniquely named and are children of the same node, you can use runAction:onChildWithName:, as follows:

SKAction *action = [SKAction sequence:
    @[[SKAction runAction:[SKAction moveTo:CGPointMake(200.0f, 200.0f) duration:1.0f]
          onChildWithName:@"NODEA"],
      [SKAction waitForDuration:2.0f],
      [SKAction runAction:[SKAction moveTo:CGPointMake(200.0f, 200.0f) duration:1.0f]
          onChildWithName:@"NODEB"]]];

[parent runAction:action];

More generally, you can use runBlock: to do pretty much anything as a step in an SKAction sequence:

SKAction *action = [SKAction sequence:
    @[[SKAction runBlock:^{
          [nodeA runAction:[SKAction moveTo:CGPointMake(200.0f, 200.0f) duration:1.0f]];
      }],
      [SKAction waitForDuration:2.0f],
      [SKAction runBlock:^{
          [nodeB runAction:[SKAction moveTo:CGPointMake(200.0f, 200.0f) duration:1.0f]];
      }]]];

[parent runAction:action];
godel9
  • 7,340
  • 1
  • 33
  • 53
  • Note that `runBlock:` and `runAction:onChildWithName:` both run with an "instantaneous duration" according to the docs, so this sequence would run everything at once if you didn't have the `waitForDuration:` action in there. – Craig Brown Jul 01 '18 at 11:45