gpt4 book ai didi

Swift let 属性

转载 作者:行者123 更新时间:2023-11-28 07:56:57 26 4
gpt4 key购买 nike

我需要一些调试帮助,因为我遇到的错误真的很难。

这是一款具有复杂动画的游戏。然而,问题不在于 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}}

最佳答案

好吧,问题是由我创建组操作的方式引起的:

let gravOperation = ActionOperation(node: piecesLayer, action: SKAction.group(groupActions))

groupActions 数组为空时,将永远不会在节点上执行操作,如果我们调用:

node.run(badGroupAction, completion: { /* never called */ })

永远不会调用completion。这就是队列卡住的原因。我认为这是一个 SpriteKit 缺陷。如果我们尝试运行空组或序列,我更希望立即调用 completion

为什么 action 变成了 uninitialized?我不知道,我认为这是一个错误,可能与空数组有关。

关于Swift <uninitialized> let 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47791334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com