SwiftUI应用程序在再次激活后重置场景
每次我的应用程序进入后台然后再次激活时,SpriteView总是会重置,场景会返回到关卡的开头。我怎样才能阻止这一切,或者我做错了什么 我的代码:SwiftUI应用程序在再次激活后重置场景,swiftui,sprite-kit,spriteview,Swiftui,Sprite Kit,Spriteview,每次我的应用程序进入后台然后再次激活时,SpriteView总是会重置,场景会返回到关卡的开头。我怎样才能阻止这一切,或者我做错了什么 我的代码: struct GameContainer: View { private var scene: SKScene { let scene = Splash() scene.size = Size.shared.setupSceneSize() scene.scaleMode = .aspectF
struct GameContainer: View {
private var scene: SKScene {
let scene = Splash()
scene.size = Size.shared.setupSceneSize()
scene.scaleMode = .aspectFit
return scene
}
var body: some View {
SpriteView(scene: scene, options: .ignoresSiblingOrder)
.edgesIgnoringSafeArea(.all)
.statusBar(hidden: true)
}
}
struct Game: View {
...
var body: some View {
NavigationView {
ZStack {
GameContainer()
....
更新:
struct GameContainer: View {
static var scene = Splash()
var body: some View {
SpriteView(scene: GameContainer.scene, options: .ignoresSiblingOrder)
.edgesIgnoringSafeArea(.all)
.statusBar(hidden: true)
}
}
class Splash: SKScene {
override init() {
super.init(size: Size.shared.setupSceneSize())
scaleMode = .aspectFit
}
....
}
尝试在
Splash()
内设置场景的大小和缩放模式,然后将场景设置为静态。我认为这应该可以解决SpriteView一直在重新加载的问题
struct GameContainer: View {
static var scene = Splash()
var body: some View {
SpriteView(scene: GameContainer.scene, options: .ignoresSiblingOrder)
.ignoresSafeArea()
.statusBar(hidden: true)
}
}
struct Game: View {
...
var body: some View {
NavigationView {
ZStack {
GameContainer()
....
我试过了,但结果还是一样。@JamesCastrejon我在自己的应用程序中使用了这个策略,所以我知道它是有效的。我和你有同样的问题。我建议您再看看我所描述的实现。此外,您还可以尝试将GameContainer()
置于NavigationView
之外。不确定这是否会改变什么,但我自己的实现中没有NavigationView
,所以可能这就是区别。谢谢你的建议。导航视图之所以存在,是因为我是如何设计我的游戏的。我今天尝试了两次,但在没有导航视图的情况下/在导航视图之外它仍然不起作用。到底是什么不起作用?你能说得更具体一点吗?我对sprite工具包不太熟悉,但你的场景是一个计算属性,每次访问它时,它都会被计算/重新创建。