ios – 在两个或更多SKSpriteNode上同步SKActions?

前端之家收集整理的这篇文章主要介绍了ios – 在两个或更多SKSpriteNode上同步SKActions?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想同步移动两个(或更多)SKSpriteNodes.任何差异都会显示出来我试图按顺序触发每个精灵的SKAction,当最后一个精灵完成时,它会触发一个新的动作.但事实证明,这些动作并没有以它们开始的相同顺序结束,这导致了一个明显的时间差异.

有没有办法在两个或多个具有持续时间的精灵上运行并行SKActions,以便它们在完全相同的时间结束或者至少以它们的启动顺序结束?

这是一个不起作用的原则示例:

- (void)testMethod1{
SKSpriteNode *child_1=[arrayWithSprites objectAtIndex:1];
SKSpriteNode *child_2=[arrayWithSprites objectAtIndex:2];

//This doesn't work.
[child_1 runAction:[SKAction moveToX:20.0 duration:0.5]];
[child_2 runAction:[SKAction moveToX:20.0 duration:0.5] 
    completion:^{[self testMethod1];}];
//Actions might not be finished in the order they are started.
}

这是我尚未尝试的一种方式,但想知道它是否可以解决我的问题:

- (void)testMethod2{
SKSpriteNode *child_1=[arrayWithSprites objectAtIndex:1];
SKSpriteNode *child_2=[arrayWithSprites objectAtIndex:2];

//Will this guarantee total syncronisation?
[self runAction:[SKAction group:[NSArray arrayWithObjects:
                                 [SKAction runBlock:^{[child_1 runAction:[SKAction moveToX:20.0 duration:0.5]];}],[SKAction runBlock:^{[child_2 runAction:[SKAction moveToX:20.0 duration:0.5]];}],nil]]
      completion:^{[self testMethod2];}];
}

我希望我的英语和思想是可以理解的.

// Micke ….

解决方法

只需添加一个容器SKNode,然后它们将立即移动:

SKNode *containerNode = [[SKNode alloc] init];    
[containerNode addChild:node1];    
[containerNode addChild:node2]; //add as many as necessary    
[containerNode runAction:someAction]; //declare the action you want them both to perform

猜你在找的Xcode相关文章