0

我的问题是我不知道如何通过滑动来旋转精灵。当我向左滑动时,我想顺时针旋转它,当我向右滑动时,我想以其他方式旋转。我滑动的速度有多快,它旋转的速度有多快,然后它会减速并停止。我试图寻找答案,但没有希望。谁能帮帮我吗?

这是我使用的代码。当我向左触摸时,精灵围绕中心顺时针旋转,并以其他方式触摸向右。我只是不知道如何计算滑动速度并用它来计算旋转速度。

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)
}
}
4

2 回答 2

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

}
于 2015-10-26T02:45:10.027 回答
0

你不能应用 SKAction 来旋转你的精灵吗?

            let action = SKAction.rotateByAngle(100, duration: 0.1)
            yourSprite.runAction(action, completion: {})

根据您的滑动速度计算度数和持续时间的 SKAction 参数。

于 2015-10-25T21:22:17.587 回答