Xcode 7 Sprite Kit X 坐标在 iOS 模拟器中偏离中心

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

我刚刚更新到Xcode 7 / Swift 2,我遇到了这个问题,iOS模拟器中的SpriteKit中的x轴似乎偏离了中心。请有心人试一下验证一下,很简单。在 Xcode 7 中创建一个新的 SpriteKit 项目,并在 touchesBegan 方法中,在 GameScene.swift 文件中的 let location = touch.locationInNode(self) 下面添加这段代码 print(location)

然后 运行 它在 iOS 模拟器(任何设备,尽管我选择了 iPhone 4s)并在查看输出结果的同时在视图中单击。 Y 轴在底部为零,但 X 轴在左侧似乎约为 300,并随着向右移动而增加。我为此失去了理智!

整个 touchesBegan 方法如下所示:

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)
    }
}

让我知道您是否可以重现它,以及为什么会这样!

尝试在 GameViewController 的 viewWillLayoutSubviews() 方法中添加下一行:

scene.size = skView.bounds.size

我在 scene.scaleMode = .AspectFill 行之前添加了它。这是否解决了问题?

请在找到下一段代码后找到GameViewController.swift

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)
}

所以你必须添加:

 scene.size = UIScreen.mainScreen().bounds.size

行后:

scene.scaleMode = .AspectFill

这将解决您的问题。 希望对你有帮助