struct ContentView: 查看 {@StateObject var firstscene = FirstScene()@StateObject var secondscene = SecondScene()@State var changeScene = falsevar主体:一些视图{ZStack{如果改变场景{SpriteView(场景:第一个场景)} 别的 {SpriteView(场景:第二场景)}按钮 {changeScene.toggle()} 标签: {文本(更改")}}}}//使用SKscene的子类创建的2个SKScene类 FirstScene: SKScene, ObservableObject {让 firstScene = SKScene(fileNamed: "FirstScene")覆盖 func didMove(查看:SKView){场景?.视图?.presentScene(firstScene)}}类 SecondScene: SKScene, ObservableObject {让 secondScene = SKScene(文件名:SecondScene")覆盖 func didMove(查看:SKView){场景?.视图?.presentScene(secondScene)}}现在我的疑问是,我正在使用 var changeScene 更改 contentView 中的 SpriteView,可以使用另一种方法以其他方式完成相同的事情吗?对任何建议持开放态度,我正在尝试理解这个框架.谢谢 解决方案 您正在使用标准的视图切换方式.你可以像这样清理你的代码:struct ContentView: 查看 {@State var changeScene = falsevar主体:一些视图{ZStack{SpriteView(scene: (changeScene ? FirstScene() : SecondScene()))//这是一个三元运算符按钮 {changeScene.toggle()} 标签: {文本(更改")}}}}新视图不需要是@StateObjects.您可以直接在视图中调用视图类型.注意:我不在电脑旁,所以没有测试.I wrote this simple few line of code to swap between 2 scene in Swiftui using SpriteKit, I'm try to understand if there could be a (better) different way to change from one scene to another using a button.struct ContentView: View { @StateObject var firstscene = FirstScene() @StateObject var secondscene = SecondScene() @State var changeScene = false var body: some View { ZStack{ if changeScene { SpriteView(scene: firstscene) } else { SpriteView(scene: secondscene) } Button { changeScene.toggle() } label: { Text("Change") } } }}// the 2 SKScene created using a sub class of SKsceneclass FirstScene: SKScene, ObservableObject { let firstScene = SKScene(fileNamed: "FirstScene") override func didMove(to view: SKView) { scene?.view?.presentScene(firstScene) }}class SecondScene: SKScene, ObservableObject { let secondScene = SKScene(fileNamed: "SecondScene") override func didMove(to view: SKView) { scene?.view?.presentScene(secondScene) }}now my doubt is, I'm change the SpriteView in the contentView using a var changeScene, could be done the same things in some other way using another approach?open to any suggestions, I'm try to understand this framework .thanks 解决方案 You are using a standard way of switching views. You could clean your code up a bit like this:struct ContentView: View { @State var changeScene = false var body: some View { ZStack{ SpriteView(scene: (changeScene ? FirstScene() : SecondScene())) // this is a terniary operator Button { changeScene.toggle() } label: { Text("Change") } } }}The new views do not need to be @StateObjects. You can call the View Types directly in your view.Note: I am away from my computer so this is not tested. 这篇关于Spritekit 和 swiftui,更好地改变场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-05 05:26