ios - SpriteKit 中滑动旋转 Sprite 节点

标签 ios swift rotation sprite-kit rotateanimation

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

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

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

最佳答案

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

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

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

关于ios - SpriteKit 中滑动旋转 Sprite 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33333147/

相关文章:

ios - ProgressHUD.showSuccess ("Success") 给出链接错误

arrays - 如何使用最高的 Int 值对字典进行排序或过滤?

python - OpenCV Python围绕特定点将图像旋转X度

graph - 绘制垂直图,gnuplot。旋转 xlabel 和 key

Android:旋转更改后对话框等恢复

ios - 为什么当我更改其背景图像时,按钮会返回到其原始位置?

ios - Swift - 节点被触摸后延迟,直到它可以再次被触摸

ios - iphone 应用程序开发新手 - 将 webapp 服务器连接到 iphone 应用程序服务器

xcode - 是否可以在 XCode 中使用两台 iPad 测试我的应用程序?

ios - Swift 4 中传递给不带参数的调用的参数