|
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.
|
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and more!
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.