SwiftUI SpriteView 问题 - 停止添加节点

问题描述

With score calculation

Score increment commented

My GitHub Solution

我复制了此代码示例 YouTuve: How to integrate SpriteKit using SpriteView – SwiftUI 或相同的教程: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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)