How to change SKscene size
1) If you use .sks files (the spriteKit visual editor)
Just go to the GameScene.sks file (not swift) in you project where you can change the scene size in the inspector on the right.
You can check this article here where they show this if you scroll down a tiny bit. Its a good tutorial you should ready if you plan on working with the visual editor.
https://www.raywenderlich.com/118225/introduction-sprite-kit-scene-editor
2) Not using .sks files, loading scenes in code
If you dont use .sks files you can delete the GameScene.sks file and load the scene like so.
override func viewDidLoad() {
super.viewDidLoad()
// Configure the view.
guard let skView = self.view as? SKView else { return }
let scene = GameScene(size: CGSize(width: 1920, height: 1080))
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
Hope this helps
How to change SKScene size when rotate Device?
I fixed it by changing the code to :
var scene: FractalTreeScene!
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
if UIApplication.shared.statusBarOrientation.isLandscape {
scene.size = CGSize(width: 1337, height: 750)
} else {
scene.size = CGSize(width: 750, height: 1337)
}
scene.drawTree()
}
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
scene = FractalTreeScene()
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
if UIApplication.shared.statusBarOrientation.isLandscape{
scene.size = CGSize(width: 1337, height: 750)
} else {
scene.size = CGSize(width: 750, height: 1337)
}
// Present the scene
view.presentScene(scene)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
SKScene Viewable Size smaller than View bounds
I think this might be because Apple has mistakenly removed the launch screen from the iOS-only SpriteKit game template (it's still there in the multi-platform template). You need to either add and assign a launch screen or choose Main as your launch screen.
The launch screen defines the working area for the app, so without it the view bounds and scene scale mode will not fix this issue.
More info in Background is not filling the whole view SpriteKit.
How to change SKScene position before presenting?
Well I figured how to achieve this, I am sharing in case it would help someone else.
Inside AppDelegate
you get the correct safe area values and store it in a singleton (in my case: safeAreaHeight
) for access throughout the app. Subtract the amount from scene height (sceneHeight
).
let kWindow = UIApplication.shared.windows[0]
safeAreaHeight = kWindow.safeAreaInsets.top + kWindow.safeAreaInsets.bottom
screenHeight = screenRect.size.height - safeAreaHeight
Don't forget the adjust the sceneHeight
in GameViewController before presenting it:
if let view = self.view as! SKView? {
// Load the SKScene from 'MainMenuScene.sks'
if let scene = SKScene(fileNamed: "MainMenuScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFit
scene.size.height = sceneHeight
scene.size.width = sceneWidth
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
Inside loaded scene, in this case it is MainMenuScene
, you simply find subview of SKScene and adjust its Y position by subtracting the safeAreaHeight
at its center
guard let subView = self.view else { return }
subView.center.y -= safeAreaHeight
PS.: Do not adjust all the views in different scenes (ie.: GameScene, GameOverScene etc.) Once you adjust, it remains throughout the app.
How can I obtain an SKScene's size in `didMove(to view: SKView)` when setting scaleMode to resizeFill?
you aren't calling super.didMove(to:view)
. Try that first, if that doesn't work, then do:
override func didMove(to view: SKView) {
super.didMove(to:view)
DispatchQueue.main.async{
print(self.size)
}
}
What this will do is tell the GCD to run this block shortly after your function ends.
How To Change The Size Of The Window For A SpriteKit Project
I apologize...this was a stupid question, but hopefully this answer will help someone out.
I always thought that the anchor point of a SKScene was (0, 0), but it appear to be (0.5, 0.5). I'm not sure if it was always like that or if it has changed over the last year or two. Either way, adding [scene setAnchorPoint:CGPointMake(0, 0)];
after the scene is created in the ViewController
class fixed it all making the bottom left of the screen (0, 0). Hope this helps someone.
Related Topics
Landscape Orientation for Collection View in Swift
Animate a Change in Part of an Nsmutableattributedstring
Swift Swipe Navigation Table Views
How to Reuse Struct on Swift? or Is There Any Other Way
Rxswift - Generic Parameter 'Self' Could Not Be Inferred
Error When Trying to Save a Captured Image in Swift
Spritekit, Swift 2.0 - Scrollview in Reverse
Passing Data Between Views in One Viewcontroller in Swift
Creating a Custom UI View Class to Use as a Map Annotation
Google Cloud Messaging Bridging Header Import Fails
Lineargravityfield() Is Not Affecting Physics Bodies in the Scene Scenekit
Remove Separator Line of Single Row in Eureka
Draw Button on Top of Avplayer
Swift 2 Cannot Invoke 'Fseventstreamcreate' with an Argument List of Type