我正在尝试旋转箭头以跟随手指移动,但它的表现很奇怪。它绝对不是跟随它。我试图在 touchesMoved 中做到这一点。我试着这样做:
var fingerLocation = CGPoint()
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
fingerLocation = touch.locationInNode(self)
let currentOrient = arrow.position
let angle = atan2(currentOrient.y - fingerLocation.y, currentOrient.x - fingerLocation.y)
let rotateAction = SKAction.rotateToAngle(angle + CGFloat(M_PI*0.5), duration: 0.0)
arrow.runAction(rotateAction)
}
}
也试过这个:
var fingerLocation = CGPoint()
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
fingerLocation = touch.locationInNode(self)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
var radians = atan2(fingerLocation.x, fingerLocation.y)
arrow.zRotation = -radians
}
我也试过 SKConstraint.orientToPoint 但也没有成功。我究竟做错了什么?类似问题的每个答案都是建议 atan2,但它似乎对我不起作用。
最佳答案
如果你想将 Sprite 旋转到触摸位置,它应该很简单:
let touchLocation = touch.locationInNode(self)
var dx = hero.position.x - positionInScene.x;
var dy = hero.position.y - positionInScene.y ;
var angle = atan2(dy,dx) + CGFloat(M_PI_2)
hero.zRotation = angle
当我尝试时它起作用了,所以它可以让您基本了解从哪里开始。或者我误解了你想要达到的目标......
编辑:
目前,如果您尝试将角度转换为度数,您将得到 -90 到 270 度范围内的角度。它描述了here为什么。如果您想使用 0 到 360 度范围内的角度,您可以将上面的代码更改为:
var dx = missile.position.x - positionInScene.x ;
var dy = missile.position.y - positionInScene.y;
var angleInRadians = atan2(dy,dx) + CGFloat(M_PI_2)
if(angleInRadians < 0){
angleInRadians = angleInRadians + 2 * CGFloat(M_PI)
}
missile.zRotation = angleInRadians
var degrees = angleInRadians < 0 ? angleInRadians * 57.29577951 + 360 : angleInRadians * 57.29577951
这是调试数据的结果:
关于ios - 节点旋转不跟随手指,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32417157/