我需要一些调试帮助,因为我遇到的错误真的很难。
这是一款动画复杂的游戏。但问题不在于SpriteKit
。我希望动画按照严格的顺序相互关注,所以我实现了Operation
的子类:
class ActionOperation: Operation
{
var debugLabel: String?
private(set) var actionNodes: Set<ActionNode>
//... Other vars, isFinished etc.
init(node: SKNode, action: SKAction) {
actionNodes = [ActionNode(node: node, action: action)]
super.init()
}
init(nodesAndActions: [(SKNode?, SKAction)]) {
actionNodes = Set(nodesAndActions.map( {(tuple) in return
ActionNode(node: tuple.0, action: tuple.1)
}))
super.init()
}
override func start() { /* ... */ }
}
对于此课程详情,您可以看到the source question。 辅助结构:
extension ActionOperation {
struct ActionNode: Hashable {
static func ==(lhs: ActionOperation.ActionNode, rhs: ActionOperation.ActionNode) -> Bool {
return lhs.node == rhs.node && lhs.action == rhs.action
}
weak var node: SKNode?
// This constant is causing problems!
let action: SKAction
let setIdForHash: Int
var hashValue: Int { return setIdForHash ^ action.hashValue }
}
}
ActionOperation
的实例已添加到animationQueue
。队列设置:
fileprivate let animationQueue = OperationQueue()
// Setup:
animationQueue.maxConcurrentOperationCount = 1
animationQueue.qualityOfService = .userInteractive
操作设置:
let duration = 0.35
var groupActions = [SKAction]()
for n in 0..<from.count {
let fromIndex = from[n]
let toIndex = to[n]
let move = SKAction.move(to: fieldPosition(at: toIndex), duration: duration)
let seq = SKAction.sequence([
SKAction.run(move, onChildWithName: "\(fromIndex)"),
SKAction.wait(forDuration: duration),
SKAction.run({
self.piece(at: fromIndex)?.name = "\(toIndex)"
})
])
groupActions.append(seq)
}
let gravOperation = ActionOperation(node: piecesLayer, action: SKAction.group(groupActions))
gravOperation.debugLabel = "gravity"
animationQueue.addOperation(gravOperation)
有时这个队列得到堆栈,这意味着一个操作永远执行。我尝试使用Xcode命令行调试它并输入:
p (animationQueue.operations[0] as! ActionOperation).actionNodes.first!.action
(SKAction) $R6 = <uninitialized>
这是什么意思?结构中的let
常量如何uninitialized
?
如果我打印node
,一切正常:
po (animationQueue.operations[0] as! ActionOperation).actionNodes.first!.node
<SKNode> name:'(null)' position:{0, 0} scale:{1.00, 1.00} accumulatedFrame:{{178.13499450683594, -26.469999313354492}, {908.33502197265625, 1012.9400024414062}}
答案 0 :(得分:0)
嗯,这个问题是由我的小组行动方式引起的:
let gravOperation = ActionOperation(node: piecesLayer, action: SKAction.group(groupActions))
当groupActions
数组为空时,将永远不会在节点上执行操作,如果我们调用:
node.run(badGroupAction, completion: { /* never called */ })
永远不会调用completion
。这就是为什么队列被卡住的原因。我认为这是一个SpriteKit
漏洞。如果我们尝试运行空组或序列,我希望立即调用completion
。
为什么action
成为uninitialized
?我不知道,我认为这是一个错误,可能与空数组有关。