mogelbuster
mogelbuster

Reputation: 1076

Xcode 7 Sprite Kit X Coordinate Off-Center in iOS Simulator

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

Answers (2)

wm.p1us
wm.p1us

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

RoberRM
RoberRM

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

Related Questions