ios - 节点旋转不跟随手指

标签 ios rotation sprite-kit

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

相关文章:

CSS 旋转功能。如何指定纺纱方向?

ios - 我如何获取并删除 SKNode 的父节点?

ios - 在 UIStackView 上指定 z 顺序(sendSubviewToBack 等)

java - 在 JavaFX 中获取图像/变换/形状以旋转以跟随鼠标光标

WPF:使用旋转方 block 进行碰撞检测

swift - 实现游戏对象的特定跳跃行为

ios - 如何更改 SKLabelNode 的文本?

objective-c - 发生viewdidappear后加载uitableview数据?

python - 如何使用 HTTP header 发送非英文 unicode 字符串?

ios - 如何检测父类(super class)的实例是否是子类?