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

标签 ios objective-c xcode sprite-kit

我想同步移动两个(或更多)SKSpriteNode。任何差异都会显示出来。我尝试按顺序为每个 Sprite 触发 SKAction,当最后一个 Sprite 完成时,它会触发一个新 Action 。但事实证明,操作的结束顺序与它们开始的顺序不同,这会导致明显的时间差异。

有没有办法在两个或多个具有持续时间的 Sprite 上并行运行 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];}];
}

我希望我的英语和想法是可以理解的。

//米克....

最佳答案

只需添加一个容器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

关于ios - 在两个或多个 SKSpriteNode 上同步 SKActions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20935817/

相关文章:

ios - UIWebView 身份验证示例

objective-c - 判断 NSSlider 旋钮在连续模式下为 'let go' 时

ios - NSDictionary 未获取第一项 JSON

ios - 构建 XCTest 时找不到文件错误

ios - App审核时Apple是如何检测UDID访问的?

ios - 在启动之前了解街景是否适用于特定的纬度/经度

swift - 使用 Alamofireimage 从服务器获取图像后,Imageview 未更新

xcode - Flurry 集成构建失败 : Undefined symbols for architecture armv7

ios - 为 `ECSlidingViewController` 设置子类

objective-c - 检测对 iOS 有效的 URL