I have a simple SwiftUI view with a Picker:
and a SKScene with an custom init():
I would like to know how to pass in the current value of selection, after the picker has changed it, to the GameScene instead of the initially set value being passed in all the time.
Code Block Swift struct ContentView: View { @State private var selection = 0 @State private var showingGame = false var body: some View { VStack { Picker("Choose option", selection: $selection) { ForEach(0 ..< 10, id: \.self) { Text("Option \($0)") } } Button("Play") { showingGame = true } .fullScreenCover(isPresented: $showingGame) { GeometryReader { geometry in SpriteView(scene: scene(size: geometry.size, preferredFramesPerSecond: 120) .frame(width: geometry.size.width, height: geometry.size.height) } .ignoresSafeArea() } } } func scene(size: CGSize) -> SKScene { let scene = GameScene(size: size, selection: selection) scene.size = size scene.anchorPoint = CGPoint(x: 0.5, y: 0.5) scene.scaleMode = .aspectFill return scene } }
and a SKScene with an custom init():
Code Block Swift class GameScene: SKScene { var size: CGSize var selection: Int let player: SKSpriteNode ... init(size: CGSize, selection: Int) { self.size = size self.selection = selection player = SKSpriteNode(imageNamed: "player\(selection)") super.init(size: size) } ... }
I would like to know how to pass in the current value of selection, after the picker has changed it, to the GameScene instead of the initially set value being passed in all the time.