I have a SKScene presented in a SwiftUI view. Trying to use SwiftUI buttons to add a Spritenode to the scene with a simple function.
If I run the function from the GameScene it works fine of course.
But if I try to access the function from the SwiftUI button nothing happens. The function runs (Print"image tapped" is in debug window) but the Sprite does not appear.
Why is this and how do I fix that?
My code
import SwiftUI
import SpriteKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
//ball()
}
func ball() {
let ball = SKShapeNode(circleOfRadius: 30)
ball.position = CGPoint(x: 100, y: 100)
ball.fillColor = .blue
self.addChild(ball)
}
}
struct ContentView: View {
var scene: SKScene {
let scene = GameScene()
scene.size = CGSize(width: 800, height: 930)
scene.scaleMode = .fill
scene.backgroundColor = .red
return scene
}
var body: some View {
VStack {
HStack {
Button {
let makeBall = GameScene()
makeBall.ball()
print("Image tapped!")
} label: {
Image("playButton")
}
Button {
print("Image tapped!")
} label: {
Image("pauseButton")
}
}
SpriteView(scene: scene)
.frame(width: 800, height: 930, alignment: .center)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}