【发布时间】: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