ios - 如何通过拖动旋转 Sprite

标签 ios swift sprite-kit

<分区>

当我在屏幕上拖动手指时,我试图旋转 Sprite 。所以我想要的是当我向左拖动手指时, Sprite 向右旋转,反之亦然,我不知道该怎么做。谁能帮忙。 我试过使用函数“touchesMoved”,但我不确定我应该把代码放在哪里还是应该放在新函数中还是什么。

附言。抱歉,我对 spritekit、swift 和 xcode 很陌生

最佳答案

您可以使用 SKSpriteNode.zRotation 属性来旋转 Sprite 。 并使用 touchesBegantouchesMoved 函数来跟踪滑动距离。

var previousPoint : CGPoint!

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

   previousPoint = (touches.anyObject() as UITouch).locationInView(self.view)
}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {

    let currentPoint = (touches.anyObject() as UITouch).locationInView(self.view)

    let distance = currentPoint.x - previousPoint.x

    previousPoint = currentPoint

    sprite.zRotation = sprite.zRotation + distance/100.0

}

关于ios - 如何通过拖动旋转 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27976770/

相关文章:

ios - 等待 10 秒后未能返回 - UIWebView loadHTMLString

macos - 如何快速获取分辨率更改事件?

ios - 键盘下的文本字段

ios - 如何在SKSpriteNode中 "center"SKTexture

swift - CGRectGet 不能正常工作

ios - 将 NSArray 放入 TableView

ios - swift 中的性能测试冒泡排序需要很长时间

Javascript matchMedia 在 iPhone X + safari 上返回相反的值

swift - 无法将类型 'Result<Data, Error>' 的值转换为闭包结果类型 'Result<T, E>'

swift - SKEmitterNode HSV 系列