Reputation: 1076
I just updated to Xcode 7 / Swift 2, and I came across this problem, the x-axis in SpriteKit in iOS simulator seems to be off-center. Someone please try this to verify, it is very simple. Make a new SpriteKit Project in Xcode 7, and in the touchesBegan
method, add this bit of code print(location)
just underneath let location = touch.locationInNode(self)
in the GameScene.swift file.
Then run it in iOS Simulator (any device, though I chose the iPhone 4s) and click around in the view while watching the results in the output. The Y-axis is zero at the bottom, but the X-axis seems to be around 300 on the left, and increases as you go right. I am losing my mind over this!
The whole touchesBegan
method will look like this:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
print(location) // <-- *** ADD THIS LINE ***
let sprite = SKSpriteNode(imageNamed:"Spaceship")
sprite.xScale = 0.5
sprite.yScale = 0.5
sprite.position = location
let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)
sprite.runAction(SKAction.repeatActionForever(action))
self.addChild(sprite)
}
}
Let me know if you can reproduce it, and why it is happening!
Upvotes: 0
Views: 316
Reputation: 2059
Please find GameViewController.swift, after find next chunk of code:
if let scene = GameScene(fileNamed:"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)
}
So there you have to add:
scene.size = UIScreen.mainScreen().bounds.size
after line:
scene.scaleMode = .AspectFill
that will solve your problem. hope it helps
Upvotes: 1
Reputation: 883
Try adding this next line in the viewWillLayoutSubviews()
method of your GameViewController:
scene.size = skView.bounds.size
I added it right before the line scene.scaleMode = .AspectFill
. Did this fix the problem?
Upvotes: 1