|
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:
And now my coding for adding that GameScene to my SwiftUI programming:
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:
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. |
|
|
|
Not sure if this is still a problem for you, but in case anyone is interested the solution I found was: Wrap the view in a GeometryReader - you'll most likely want to have it ignore the safe areas. Actually re-create a scene (rather than re-use) if the GeometryReader provides different values (This is from my code, but not too different from what you posted) So given my custom GameScene, the code look something like
Here I'm using all the horizontal space and half of the vertical space for my SpriteView. If I rotate, a whole new scene is created, but it will have the new dimensions. |
|
As mentioned in Apple docs you should set scene scaleMode to resizeFill. You may handle size changing overriding the didChangeSize(_ oldSize: CGSize) function in your scene. I provide a scene using ViewModel as @StateObject.
|
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.