我使用 NSOperation 子类来获取串行执行SKAction
正如这个问题中所描述的:如何在 Swift 中子类化 NSOperation 以将 SKAction 对象排队以进行串行执行? https://stackoverflow.com/q/28373490/540780
然后,我修改了之前的子类,以便对多个节点的动画进行分组:https://stackoverflow.com/a/30600718/540780 https://stackoverflow.com/a/30600718/540780
最后,因为我需要在之后运行完成块SKAction
完成后,我通过使用结构数组而不是元组数组稍微修改了代码。
struct ActionData {
let node:SKNode
let action:SKAction
let completion: () -> ()
init (node:SKNode, action:SKAction, completion:() -> () = {}) {
self.node = node
self.action = action
self.completion = completion
}
}
class ActionOperation : NSOperation
{
let _theActions:[ActionData]
var _finished = false // Our read-write mirror of the super's read-only finished property
var _executing = false // Our read-write mirror of the super's read-only executing property
var _numberOfOperationsFinished = 0 // The number of finished operations
/// Override read-only superclass property as read-write.
override var executing:Bool {
get { return _executing }
set {
willChangeValueForKey("isExecuting")
_executing = newValue
didChangeValueForKey("isExecuting")
}
}
/// Override read-only superclass property as read-write.
override var finished:Bool {
get { return _finished }
set {
willChangeValueForKey("isFinished")
_finished = newValue
didChangeValueForKey("isFinished")
}
}
// Initialisation with one action for one node
//
// For backwards compatibility
//
init(node:SKNode, action:SKAction) {
let donnees = ActionData(node: node, action: action, completion: {})
_theActions = [donnees]
super.init()
}
init (lesActions:[ActionData]) {
_theActions = lesActions
super.init()
}
func checkCompletion() {
_numberOfOperationsFinished++
logGeneral.debug(">> Block completed: \(self._numberOfOperationsFinished)/\(self._theActions.count) " + self.name!)
if _numberOfOperationsFinished == _theActions.count {
self.executing = false
self.finished = true
logGeneral.debug("Operation Completed: " + self.name!)
}
}
override func start()
{
if cancelled {
finished = true
return
}
executing = true
if name == nil {
name = "unknown"
}
_numberOfOperationsFinished = 0
var operation = NSBlockOperation()
var compteur = 0
for actionData in _theActions {
compteur++
var actionName = "???"
if let name = actionData.node.name {
actionName = name
}
logGeneral.debug("operation : \(compteur)/\(self._theActions.count) " + self.name! + " " + actionName)
operation.addExecutionBlock({
actionData.node.runAction(actionData.action,completion:{
actionData.completion()
self.checkCompletion() })
})
}
logGeneral.debug("Execute: " + self.name!)
NSOperationQueue.mainQueue().addOperation(operation)
}
}
主要思想是通过添加数据来制作动画ActionData
类型,将它们附加到一个数组并将该数组传输到ActionAnimation
object.
在一些随机情况下,一些runAction
未完成:它们开始,但其中一些随机未完成。这是一个典型的日志,其中 6 个区块已开始,但仅 5 个已完成:
start(): operation : 1/6 ???
start(): operation : 2/6 suppressionPion1
start(): operation : 3/6 suppressionPion2
start(): operation : 4/6 suppressionPion3
start(): operation : 5/6 suppressionPion4
start(): operation : 6/6 suppressionGroupe1
start(): Execute: animerSupprimer
checkCompletion(): >> Block completed: 1/6
checkCompletion(): >> Block completed: 2/6
checkCompletion(): >> Block completed: 3/6
checkCompletion(): >> Block completed: 4/6
checkCompletion(): >> Block completed: 5/6
在这种情况下只有一个runAction
未能完成,其他情况为 2、3 或无。
我实在不明白问题从何而来。
UPDATE
In some cases the app crashed with EXC_BAD_ACCESS
on main thread. This seems to be related to SKCSprite::update(double)
: