节点旋转不遵循手指 [英] Node rotation doesn't follow a finger

查看:112
本文介绍了节点旋转不遵循手指的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试旋转箭头以跟随手指移动,但它执行奇怪。它肯定不会跟随它。我试图在touchesMoved中做到这一点。我试图这样做:

  var fingerLocation = CGPoint()

覆盖func touchesMoved(触摸:设置< NSObject> ;,withEvent事件: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()

覆盖func touchesMoved(触摸:Set< ; NSObject>,withEvent事件:UIEvent){
for touch:触摸中的AnyObject {
fingerLocation = touch.locationInNode(self)
}
覆盖func update(currentTime:CFTimeInterval) {
/ *在每个f之前调用rame渲染* /
var radians = atan2(fingerLocation.x,fingerLocation.y)
arrow.zRotation = -radians
}

我也尝试过SKConstraint.orientToPoint,但也没有运气。我究竟做错了什么?对类似问题的每个答案都是建议atan2,但它似乎对我不起作用。

解决方案

如果你想要旋转精灵朝触摸位置,它应该很简单:

 让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度。它描述了


I'm trying to rotate an arrow to follow a finger movement but it performs weirdly. It is definitely not following it. I'm trying to do it in touchesMoved. I tried to do this:

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)

    }
}

And also tried this:

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
}

I also tried SKConstraint.orientToPoint but had no luck in it either. What am I doing wrong? Every answer to similar question is suggestion atan2, but it doesn't seem to work for me.

解决方案

If you want to rotate the sprite towards to touch location, it should be simple as :

    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

It worked when I tried, so it can give you an basic idea where to start. Or I misunderstood what you are trying to achieve...

EDIT:

Currently what you will get if you try to convert angle to degrees is angle in range from -90 to 270 degrees. Its described here why. If you want to work with angle in range of 0 to 360, you can change to code above to:

    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

Here is the result with debugging data:

这篇关于节点旋转不遵循手指的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆