如何一起为多个SKSpriteNode设置动画? [英] How to animate multiple SKSpriteNode together?

查看:358
本文介绍了如何一起为多个SKSpriteNode设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 SpriteKit 很陌生。我有一组节点,每个节点需要一起移动到不同的点,并且在为所有节点完成动画之后,我想做些其他的事情。

I am pretty new to SpriteKit. I have a set of nodes that need to move together to a different point for each node and after that animation completed for all of them I would like to do something else.

我以前是用 UIView 组件制作的。 [UIView animateWithDuration:completion:] 块提供了我需要的东西。但是在 SpriteKit 中,每个节点都有其自己的动作并可以单独设置动画。我找不到精灵节点的任何块动画。所以我无法控制动画的完成。

I was making this with UIView components before. A [UIView animateWithDuration:completion:] block was providing the thing that I needed. But in SpriteKit each node has its own action and animate by itself. I could not find any block animation for sprite nodes. So I cannot control the animation completion.

希望,我很清楚。

推荐答案

可能有一些 SKAction 类方法很有趣。第一个是 runBlock:,第二个是 group: (这允许操作并行运行)

There are a couple of SKAction class methods that may be of interest. The first being runBlock: and the second being group: (this allows actions to be run in parallel).

您可以使用 runAction:完成:

在带有完成处理程序的节点上运行操作'动画动作',以及'block action'中的完成区块。然后将阻止动作放入小组动作 中。最终,告诉该组动作运行。这将导致每个块动作同时开始。每个阻止操作的完成处理程序都会调用一个名为 checkCompletion 的方法。此方法通过调用节点上的 hasActions 来检查仍在设置动画的节点(具有特定名称属性值)。因为调用 checkCompletion 方法的节点将返回YES,所以如果只有一个节点返回YES,则所有动画都将完成。

The following code puts each 'animation action', along with completion block in a 'block action'. The block actions are then put into a 'group action'. Finally the group action is told to run. This causes each block action to start at the same time. The completion handler of each block action calls a method named checkCompletion. This method checks for nodes (with a specific name property value) still animating by calling hasActions on the node. Because the node that called the checkCompletion method will return YES, if only one node returns YES then all animations are done.

以下内容演示了这一点。

The following demonstrates this using two nodes, however it will work for more than two.

// assuming two SKNode subclasses named 'node1' and 'node2' AND 'self' is an SKScene subclass


// assign each animating node the same name (used in checkCompletion method)
node1.name = @"animating";
node2.name = @"animating";
// in this example both nodes will animate to the origin
CGPoint origin = CGPointMake(0.0, 0.0);
// make move actions for nodes
SKAction *shortMove = [SKAction moveTo:origin duration:1.2];
SKAction *longMove = [SKAction moveTo:origin duration:4.8];
// wrap nodes in their own separate block action
SKAction *moveNode1 = [SKAction runBlock:^{
    [node1 runAction:shortMove completion:^{
        NSLog(@"shortMove complete");
        [self checkCompletion];
    }];
}];
SKAction *moveNode2 = [SKAction runBlock:^{
    [node2 runAction:longMove completion:^{
        NSLog(@"longMove complete");
        [self checkCompletion];
    }];
}];
// put block actions in a group action
SKAction *groupAction = [SKAction group:@[moveNode1, moveNode2]];
// execute group action
[self runAction:groupAction];

checkCompletion方法-

checkCompletion method -

- (void)checkCompletion {

     __block int currentlyHasActions = 0;
     // check for all actions complete on animating nodes
     [self enumerateChildNodesWithName:@"animating" usingBlock:^(SKNode *node, BOOL *stop){
         if ([node hasActions]) {
             currentlyHasActions++;
             // prevent unnecessary enumeration
             if (currentlyHasActions > 1) { *stop = YES; }
         }
     }];
     if (currentlyHasActions == 1) {
         NSLog(@"Actions Finished!!!");

         // execute completion code here...

     }
}

不幸的是,以下动作将无效,因为阻止动作持续时间是瞬时的。因此,在这种情况下,小组行动的持续时间也是瞬时的。这就是采用 checkCompletion 方法的原因。

Unfortunately the following will not work as a block actions duration is instantaneous. Consequently the duration of the group action in this case is also instantaneous. This is the reason for the checkCompletion method.

[self runAction:groupAction completion:^{
     // completion code here... :(
}];

这篇关于如何一起为多个SKSpriteNode设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆