gpt4 book ai didi

ios - 节点旋转不跟随手指

转载 作者:行者123 更新时间:2023-11-29 12:14:04 25 4
gpt4 key购买 nike

我正在尝试旋转箭头以跟随手指移动,但它的表现很奇怪。它绝对不是跟随它。我试图在 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

这是调试数据的结果:

enter image description here

关于ios - 节点旋转不跟随手指,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32417157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com