微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SwiftUI SpriteView 问题 - 停止添加节点

如何解决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 举报,一经查实,本站将立刻删除。