首先触摸精灵节点识别问题

时间:2017-04-01 03:45:30

标签: ios

我正在开发简单的iOS游戏。它使用Swift 3和SpriteKit构建。想法是将大炮放置在屏幕的顶部,并且大炮的尖端旋转180度。沿着x轴,尖端指向你的手指。问题是当我第一次接触时,大炮的尖端指向离我手指大约45度。当我的手指沿x轴移动时,炮头围绕y轴旋转。

这就是我得到的: Thats how it rotates

这就是我需要的: rotation along x-axis

import SpriteKit

class GameScene: SKScene {

var cannon:SKSpriteNode!
var touchLocation: CGPoint = CGPoint.zero

override func didMove(to view: SKView) {

    cannon = self.childNode(withName: "cannon") as! SKSpriteNode
}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    touchLocation = touches.first!.location(in: self)

}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    touchLocation = touches.first!.location(in: self)
}


override func update(_ currentTime: TimeInterval) {
    let percent = touchLocation.x / size.width
    let newAngle = percent * 180 - 180
    cannon.zRotation = CGFloat(newAngle) * CGFloat(Double.pi) / 180.0
}
}

以上是来自GameScene.swift

我做错了什么???

0 个答案:

没有答案