我复制了YouTuve 的这个代码示例:如何使用 SpriteView – SwiftUI 集成 SpriteKit或在hackingwithswift.com上的相同教程
,它运行良好。之后我想显示用户点击了多少次(添加了节点)。我使用了@Binding PropertyWrapper,之后应用程序停止正常工作。我无法修复它。
如果要发表评论score += 1
- 一切都会重新开始!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let location = touch.location(in: self)
let box = SKSpriteNode(color: SKColor.red, size: CGSize(width: 50, height: 50))
box.position = location
box.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 50, height: 50))
self.addChild(box)
score += 1 //<----- If to comment this line - everything working again!
print("Nodes: \(self.children.count), score: \(score)")
}
输出:
2021-04-03 02:21:00.579404+0100 AP SwiftUI SpriteView iOS14[57663:9610434] Metal API Validation Enabled
Nodes: 1, score: 1
Nodes: 1, score: 2
Nodes: 1, score: 3
Nodes: 1, score: 4