Run SKActions sequence with different nodes

时光毁灭记忆、已成空白 提交于 2020-01-11 08:50:40

问题


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.

回答1:


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];


来源:https://stackoverflow.com/questions/25823428/run-skactions-sequence-with-different-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!