Spritekit and swiftui, change scene a better way

Viewed 484

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 SKscene


class 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

2 Answers

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.

iOS 14, Swift 5

This is tested and comes from this article.

https://betterprogramming.pub/build-a-game-of-chess-with-spritekit-3229c23bdba0

import SwiftUI import SpriteKit

struct ContentView: View {

@State var switcher = false

var scene: SKScene {
    let scene = GameScene.shared
    scene.size = CGSize(width: 256, height: 512)
    scene.scaleMode = .fill
    scene.backgroundColor = .red
    scene.name = "red"
    return scene
}

var scene2: SKScene {
    let scene2 = GameScene2.shared
    scene2.size = CGSize(width: 256, height: 512)
    scene2.scaleMode = .fill
    scene2.backgroundColor = .blue
    scene2.name = "blue"
    
    return scene2
}

var body: some View {
  if switcher {
    SpriteView(scene: scene)
        .frame(width: 256, height: 512)
        .ignoresSafeArea()
        .background(Color.red)
        .onAppear {
          scene2.isPaused = true
        }
        .onDisappear {
          scene2.isPaused = false
        }
        
  } else {
    SpriteView(scene: scene2)
        .frame(width: 256, height: 512)
        .ignoresSafeArea()
        .background(Color.blue)
        .onAppear {
          scene.isPaused = true
          
        }
        .onDisappear {
          scene.isPaused = false
        }
        
  }
  Button {
    withAnimation(.easeInOut(duration: 1.0)) {
      switcher.toggle()
    }
  } label: {
    Text("play")
  }
  
}
}
Related