Hi,
I'm trying to use SpriteKit with SwiftUI. But I'm having trouble resizing the GameScene when I use it in Swiftui.
Here is my current coding.
First, the coding for GameScene:
import SpriteKit
import GameplayKit
@objcMembers
class GameScene: SKScene {
let player = SKSpriteNode(imageNamed: "player-motorbike")
var touchingPlayer = false
override func didMove(to view: SKView) {
view.allowsTransparency = true
self.backgroundColor = .clear
if let particles = SKEmitterNode(fileNamed: "Mud") {
let farRightPt = frame.maxX // start the emitter at the far right x-point of the view
particles.advanceSimulationTime(10)
particles.position.x = farRightPt
addChild(particles)
}
let nearLeftPt = frame.minX * 3 / 4 // start the player at a quarter of the way to the far left x-point of the view
player.position.x = nearLeftPt
player.zPosition = 1
addChild(player)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// this method is called when the user touches the screen
guard let touch = touches.first else { return }
let location = touch.location(in: self)
let tappedNodes = nodes(at: location)
if tappedNodes.contains(player) {
touchingPlayer = true
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard touchingPlayer else { return }
guard let touch = touches.first else { return }
let location = touch.location(in: self)
player.position = location
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// this method is called when the user stops touching the screen
touchingPlayer = false
}
override func update(_ currentTime: TimeInterval) {
// this method is called before each frame is rendered
}
}
And now my coding for adding that GameScene to my SwiftUI programming:
import SwiftUI
import SpriteKit
import GameplayKit
struct ContentView2: View {
var scene: SKScene {
let scene = GameScene()
let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
scene.size = CGSize(width: width, height: height)
scene.scaleMode = .fill
return scene
}
var body: some View {
ZStack {
Image("road")
.resizable()
.aspectRatio(contentMode: .fill)
.ignoresSafeArea()
SpriteView(scene: scene, options: [.allowsTransparency])
.ignoresSafeArea()
}
}
}
struct ContentView2_Previews: PreviewProvider {
static var previews: some View {
ContentView2()
}
}
The coding above pushes the GameScene down so that it only occupies about a third of the screen.
I've tried other ways to resize the GameScene. The most luck I've had is with setting:
scene.size = CGSize(width: 300, height: 400)
That allows the GameScene to fill up the entire screen, but the objects show as distorted.
I think I should probably use GeometryReader here, but since the scene isn't some View, that doesn't work either.
Does anyone know how to resize an SKScene so that it can be used in SwiftUI?
Thank you.