ios - Spritekit 多节点动画序列器,可以提高性能吗?

标签 ios swift sprite-kit

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

最佳答案

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

https://github.com/evannnc/ActionQ

关于ios - Spritekit 多节点动画序列器,可以提高性能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31393854/

相关文章:

ios - 获取 BLE 设备的响应

swift - 过滤数组并在 TableView 中显示?

swift - NSSavePanel 名称不是用户可编辑的

ios - SpriteKit如何获得正确的屏幕尺寸

ios - 围绕中心质量创建圆形路径的算法?

javascript - 如何修复您应该只在 IOS 的应用程序中显式渲染一个导航器

ios - Swift 中小数到分数的转换

ios - SpriteKit 中的动画路径绘制

ios - SpriteKit : A gap between SKSpriteNodes

ios - xcode objective-c ,EXC_BAD_ACCESS