Spritekit多节点动画音序器,这可以提高性能吗?

时间:2015-07-13 21:30:57

标签: ios swift sprite-kit

我需要能够对在多个节点上运行的一组SKA序列进行排序并编写此测试。在单个节点上对多个动作进行排序很容易,但是在多个节点上操作之后没有直接支持运行序列(例如交换两个节点位置然后闪烁一些其他节点然后在更多节点中淡入淡出)。以下代码处理排序,但我想知道如果节点数太大,每个update扫描所有节点可能会花费太多时间。

class AnimatorSequence
{
    var animatorStack = [Animator]()
    var currentAnimator : Animator!

    func startAnimator()
    {
        currentAnimator = animatorStack.removeAtIndex(0)
        currentAnimator.execute()
    }

    func addAnimator( animator : Animator )
    {
        animatorStack.append(animator)
        if currentAnimator==nil && animatorStack.count == 1
        {
            startAnimator();
        }
    }

    func process()
    {
        if let anim = currentAnimator
        {
            if anim.isDone()
            {
                currentAnimator = nil
                if animatorStack.count > 0
                {
                    startAnimator();
                }
            }
        }
    }
}

let animatorSequencer = AnimatorSequence()

class Animator
{
    typealias functionType = (() -> Void)?
    var function : functionType
    var parentNode : SKNode

    init (function:functionType, parent : SKNode)
    {
        self.function = function
        self.parentNode = parent
    }

    func execute()
    {
        if let f = function
        {
            f()
        }
    }

    func isDone() -> Bool
    {
        var count = parentNode.children.count
        for node in parentNode.children
        {
            if !node.hasActions()
            {
                count--;
            }
        }
        return count==0;
    }
}

示例电话:

    let a = Animator(function: { () -> Void in

        firstDot.node.runAction(moveToSecond)
        secondDot.node.runAction(moveToFirst)

        }
        , parent : self
    )

    animatorSequencer.addAnimator(a)

和每次更新

override func update(currentTime: CFTimeInterval)
{
    animatorSequencer.process()
}

这一点是为了让我能够轻松地对多个多节点动画进行排序,只需要一个闭包,而不需要任何复杂的动画。它似乎有效,但我想知道是否有更高效的方法来确定所有SKAction是否完整。

1 个答案:

答案 0 :(得分:2)

我实际上写了一堂课来做你正在寻找的东西。它本质上是一个接一个调用的SKActions队列。您添加到队列中的任何内容都将按顺序处理,因此您不必再担心回调例程。您可以添加来自多个不同SKSpriteNodes的操作,而无需创建操作序列。

https://github.com/evannnc/ActionQ