我的问题是我不知道如何通过滑动旋转精灵。当我向左滑动时,我想顺时针旋转它,当我向右滑动时,我想以其他方式旋转它。我刷的速度有多快,它旋转的速度有多快,然后减慢并停止。我试图寻找答案,但没有希望。有人可以帮帮我吗?
这是我使用的代码。当我向左触摸时,精灵绕中心顺时针旋转,向右触摸其他方式。我只是不知道如何计算滑动的速度并用它来计算旋转速度。
import SpriteKit
enum State {
case Stopped
case Clockwise
case CounterClockwise }
let two_pi = CGFloat(M_PI*2.0)
let pi = CGFloat(M_PI)
// These are useful vector/point operators
func * (left:CGPoint, right:CGFloat) -> CGPoint {
return CGPointMake(left.x*right, left.y*right)
}
func += (inout left:CGPoint, right:CGPoint) {
left = CGPointMake(left.x+right.x, left.y+right.y)
}
func * (left:CGVector, right:CGFloat) -> CGVector {
return CGVectorMake(left.dx*right, left.dy*right)
}
func / (left:CGVector, right:CGFloat) -> CGVector {
return CGVectorMake(left.dx/right, left.dy/right)
}
class GameScene: SKScene {
let shape = SKShapeNode(circleOfRadius: 7)
let radius:CGFloat = 30
var center = CGPointZero
var currentAngle = -pi/2
let angleIncr = two_pi / 60.0
var state:State = .Stopped
let rotationOffset: CGFloat = -CGFloat(M_PI/2.0)
private var targetZRotation: CGFloat = 0
override func didMoveToView(view: SKView) {
scaleMode = .ResizeFill
// Set the center of the sling
center = CGPointMake (CGRectGetMidX(view.frame),CGRectGetMidY(view.frame))
addBall()
let circle = SKShapeNode(circleOfRadius: radius)
circle.position = CGPointMake (CGRectGetMidX(view.frame),CGRectGetMidY(view.frame))
addChild(circle)
}
// Adds a circle shape node at the bottom of the sling
func addBall() {
currentAngle = -pi/2
shape.fillColor = SKColor.blueColor()
shape.strokeColor = SKColor.clearColor()
shape.position = CGPointMake (center.x, center.y-radius)
shape.physicsBody = SKPhysicsBody(circleOfRadius: 7)
shape.physicsBody?.affectedByGravity = false
shape.physicsBody?.mass = 0.5
shape.zPosition = 1
addChild(shape)
}
func calculateAngleToTouch(touch: UITouch) {
let position = touch.locationInNode(self)
let angle = atan2(position.y-shape.position.y, position.x-shape.position.x)
targetZRotation = angle + rotationOffset
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch : UITouch = (touches.first as UITouch?)!
let touchPosition = touch.locationInNode(self)
let newRotationDirection : State = touchPosition.x < CGRectGetMidX(self.frame) ? .Clockwise : .CounterClockwise
if state != newRotationDirection && state != .Stopped {
state = newRotationDirection
} else if state == newRotationDirection {
state = .Stopped
}
else if (state == .Stopped){
state = newRotationDirection
}
calculateAngleToTouch((touches.first as UITouch?)!)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
calculateAngleToTouch((touches.first as UITouch?)!)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (state == .Clockwise || state == .CounterClockwise) {
state = .Stopped
}
}
override func update(currentTime: CFTimeInterval) {
var point = angleToPoint(currentAngle) * radius
point += center
shape.position = point
switch (state) {
case .CounterClockwise:
currentAngle -= -angleIncr
case .Clockwise:
currentAngle -= angleIncr
default:
break
}
// Wrap at 2 pi
currentAngle %= two_pi
}
func angleToPoint(angle:CGFloat) -> CGPoint {
return CGPointMake(cos(angle), sin(angle))
}
func magnitude(v1:CGVector) -> CGFloat {
return sqrt(v1.dx*v1.dx+v1.dy*v1.dy)
}
}
答案 0 :(得分:0)
你不能应用SKAction旋转你的精灵吗?
let action = SKAction.rotateByAngle(100, duration: 0.1)
yourSprite.runAction(action, completion: {})
根据您的滑动速度计算度数和持续时间的SKAction参数。
答案 1 :(得分:0)
您可以使用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
}