Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 斯威夫特,斯威夫特旋转精灵斯威夫特3_Ios_Swift_Sprite Kit - Fatal编程技术网

Ios 斯威夫特,斯威夫特旋转精灵斯威夫特3

Ios 斯威夫特,斯威夫特旋转精灵斯威夫特3,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我正在尝试使用swift、IOS 10和TouchsMoved功能旋转一个精灵,我的代码的工作原理是它移动精灵,但方向不对,等等 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesMoved(touches, with: event) guard let touch=touches.first{ return } l

我正在尝试使用swift、IOS 10和TouchsMoved功能旋转一个精灵,我的代码的工作原理是它移动精灵,但方向不对,等等

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
   super.touchesMoved(touches, with: event)
   guard let touch=touches.first{
         return
   }
   let touchLocation = touch.location(in: self)
   self.zRotation=CGFloat(atan2(Double(touchLocation.y), Double(touchLocation.x)))
}
override func touchesMoved(touch:Set,带有事件:UIEvent?){
super.touchesMoved(touches,with:event)
保护让接触=先接触{
返回
}
让touchLocation=touch.location(in:self)
self.zRotation=CGFloat(atan2(Double(touchLocation.y)、Double(touchLocation.x)))
}
有人知道使用zRotation和TouchsMoved旋转精灵的正确方法吗?
谢谢你

我想你可能忽略了SpriteKit中约束的强大功能。很容易做到,苹果在推广、展示和教育那些考虑使用SpriteKit的人方面做得很糟糕

允许你说“到这儿来”或“看看这个”

在您的情况下,“看看这个”是理想的,从文档:

方向约束 方向约束的一个常见用途是查看 约束。例如,您可以创建“注视”约束,以使 一双眼睛注视着一个移动的物体,或者在空中有一个火箭点 目标的方向

更具体地说,您可以在TouchMoved中使用这些选项:


谢谢你的建议。是的,我对这个问题的解释不太好。因此,我将在这里添加完整的代码,当我停止拖动并再次开始拖动时,它似乎会自动移动90度

我想做的是绕着另一个精灵旋转一个精灵,比如绕着轮子旋转一个炮筒。。。从场景加载控制盘时,它调用该类并添加一个子节点(桶),该子节点应能够围绕其控制盘移动

import SpriteKit
class WheelNode: SKSpriteNode, CustomNodeEvent{
  private var tubeNode=SKSpriteNode(imageNamed: "cannon-barrel")
  var previousPoint:CGPoint!
  var touchLocation:CGPoint!
  func didMoveToScene() {
    guard let scene=scene else{
        return
    }
    isUserInteractionEnabled=true
    tubeNode.anchorPoint=CGPoint(x:0, y:0)
    tubeNode.position=position
    self.addChild(tubeNode)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch=touches.first{
            previousPoint=touch.location(in: scene!)
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)
        if let touch=touches.first{
            touchLocation=touch.location(in: scene!)
            var angle=atan2(touchLocation.y-previousPoint.y, touchLocation.x-previousPoint.x)
            self.zRotation=angle
        }
    }
}
导入SpriteKit
类WheelNode:SKSpriteNode、CustomNodeEvent{
私有变量tubeNode=SKSpriteNode(图像名为:“炮筒”)
var-previousPoint:CGPoint!
var touchLocation:CGPoint!
func didmovetosene(){
守卫让场景=场景其他{
返回
}
isUserInteractionEnabled=true
tubeNode.anchorPoint=CGPoint(x:0,y:0)
管节点。位置=位置
self.addChild(tubeNode)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
previousPoint=touch.location(在:场景中!)
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
super.touchesMoved(touches,with:event)
如果让触摸=先触摸{
touchLocation=touch.location(在:场景中!)
变量角度=atan2(touchLocation.y-previousPoint.y,touchLocation.x-previousPoint.x)
自旋转=角度
}
}
}

您需要提供更多关于您正在尝试做什么的详细信息。现在你得到的是相对于场景坐标系的角度,而不是你的精灵,因此如果你的精灵不在(0,0)处,你将看不到精灵指向你手指所在的位置。这里的自我是什么?它是SKSpriteNode的一个子类吗?Self是SKSpriteNode的一个子类正如Knight0fDragon所指出的,你必须说出你到底想在这里实现什么,例如,当你在精灵上移动手指时(或以其他方式),你到底想看到什么。如果只想朝场景坐标系中的点击位置旋转精灵,请参见以下内容(瞄准部分):