我使用子类NSOperation来获取SKAction
的序列执行,如本问题所述:How to subclass NSOperation in Swift to queue SKAction objects for serial execution?
然后,我修改了上一个子类,以便为多个节点分组动画:https://stackoverflow.com/a/30600718/540780
最后,因为我需要在SKAction
完成后运行完成块,所以我使用struct数组而不是元组数组稍微修改了代码。
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
对象。
在一些随机的情况下,有些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,或者没有。
我真的不明白问题的来源。
在某些情况下,应用程序在主线程上与EXC_BAD_ACCESS
崩溃。这似乎与SKCSprite::update(double)
有关: