如何通过拖动旋转精灵
How to rotate sprite with drag
当我在屏幕上拖动手指时,我试图旋转精灵。所以我想要的是当我向左拖动手指时,精灵向右旋转,反之亦然,我不知道该怎么做。谁能帮忙。
我试过使用函数 "touchesMoved" 但我不确定我应该把代码放在哪里还是应该放在新函数中还是什么。
ps。抱歉,我是 spritekit 的新手,swift 和 xcode
您可以使用SKSpriteNode.zRotation
属性 来旋转精灵。
并使用 touchesBegan
和 touchesMoved
函数来跟踪滑动距离。
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
}
当我在屏幕上拖动手指时,我试图旋转精灵。所以我想要的是当我向左拖动手指时,精灵向右旋转,反之亦然,我不知道该怎么做。谁能帮忙。 我试过使用函数 "touchesMoved" 但我不确定我应该把代码放在哪里还是应该放在新函数中还是什么。
ps。抱歉,我是 spritekit 的新手,swift 和 xcode
您可以使用SKSpriteNode.zRotation
属性 来旋转精灵。
并使用 touchesBegan
和 touchesMoved
函数来跟踪滑动距离。
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
}