【问题标题】:Spritekit Multi-Node animation sequencer, can this be made more performant?Spritekit 多节点动画音序器,这可以提高性能吗?
【发布时间】:2015-07-13 21:30:56
【问题描述】:

我需要能够对在多个节点上运行的一组 SKAction 进行排序,并编写了这个测试。在单个节点上对多个操作进行排序很容易,但没有直接支持在多个节点上操作后运行序列一个(例如交换两个节点位置,然后闪烁一些其他节点,然后淡入更多节点)。以下代码处理排序,但我想知道如果节点数量太大,是否扫描所有节点每个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 都已完成。

【问题讨论】:

  • 您有没有找到一种既高效又酷的方法?

标签: ios swift sprite-kit


【解决方案1】:

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

https://github.com/evannnc/ActionQ

【讨论】:

    猜你喜欢
    • 2014-10-27
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 2015-07-14
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    相关资源
    最近更新 更多