Reputation: 79
I'm working with swift and SpriteKit and I'm having an issue with what I believe is my GameScene being wider than my device's screen size. What I need to do is move an image and then not allow it to move past the edges of the screen.
Where I'm at right now is that my image will collide with the top and bottom of the screen, but it will move off to the left or right and collide with the edge somewhere off screen.
I came across this question in my searches to try and fix the problem: Screen's real size is bigger than what I see
Just doing what they tried and commenting out the line that says scene.scaleMode = .AspectFill
will cause collisions with the edge of the screen to work properly, however like they stated, the image is now not scaled properly and in my case only takes up the middle leaving grey edges to each side.
Their other remedy was adding scene = GameScene(size: self.view.frame.size)
after taking out the default created scene in, I'm assuming, GameViewController.swift
but I wasn't sure what to remove to try doing that.
My viewDidLoad
currently looks like this:
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
//skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
If someone could help by pointing me in the right direction for what to remove or what else to try that might fix this, I would greatly appreciate it.
Thanks in advance!
Upvotes: 3
Views: 3392
Reputation: 2401
I have had a similar problem with Objective-C iOS 7 and 8 in SpriteKit. When replacing a SKScene
with another, setting the next SKScene
size with self.size
would cause this exact error. My solution was as simple as:
//ObjectiveC
MyScene *newScene = [[MyScene alloc] initWithSize:CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height * 2)];
newScene.scaleMode = SKSceneScaleModeAspectFill;
//Swift
var newScene : MyScene = MyScene(size:CGSize(width: self.view.frame.size.width * 2, height: self.view.frame.size.height))
newScene.scaleMode = .AspectFill
Upvotes: 0
Reputation: 13665
Try to use viewWillLayoutSubviews
instead of viewDidLoad and initialize your scene with skView.bounds.size
:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.showsPhysics = true
skView.showsDrawCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
if(skView.scene == nil){
scene.scaleMode = .AspectFill
scene.size = skView.bounds.size
skView.presentScene(scene)
}
}
}
In viewDidLoad
the final size of view may not be know yet and viewWillLayoutSubviews
is sometimes better place to initialize the scene.
When scene is loaded from .sks file it has default size of 1024x768. That's why your scene is sometimes wider than expected, and this can be changed like from the code I've posted.
If you have trouble with wrong view's size (but I doubt that) check your launch images...If those are wrongly selected, a view can have a wrong size.
Upvotes: 4
Reputation: 354
Try:
var scene = GameScene(size: UIScreen.mainScreen().bounds)
or:
var dev_w = UIScreen.mainScreen().bounds.width*2
var dev_h = UIScreen.mainScreen().bounds.height*2
var new_frame = CGRectMake(0 , 0, dev_w, dev_h)
var scene = GameScene(size: new_frame)
Upvotes: 0