Moving SKSpriteNode in a downward loop using Swift

Using Swift and SpriteKit, I'd like to move SKSpritenode in a spiral pattern, but haven't found the right resources to get me started. To be more precise, I would like to move the sprite node in a downward loop. I checked the sequence of SKActions, but since they are not executed in parallel, circular motion combined with movement will not work. I would love any hints, tutorials or snippets to set me up on the right track.

Thanx in advance, Marcus

+3


source to share


2 answers


I have put together a sample code that you can adapt for your own purposes. I based the code on the equation for the Archimedean spiral :

r = a + bθ

      

Where a

is the radius of the beginning; b

is the radius along which the spiral will increase per revolution, and t23 is the current angle.

A spiral is basically a glorified circle (IMO), so in order to move your node in a spiral, you need to compute a point on the circle using the angle, radius and center point:

func pointOnCircle(#angle: CGFloat, #radius: CGFloat, #center: CGPoint) -> CGPoint {
    return CGPoint(x: center.x + radius * cos(angle),
                   y: center.y + radius * sin(angle))
}

      



Then stretch SKAction

so you can easily create a spiral action:

extension SKAction {
    static func spiral(#startRadius: CGFloat, endRadius: CGFloat, angle 
         totalAngle: CGFloat, centerPoint: CGPoint, duration: NSTimeInterval) -> SKAction {

        // The distance the node will travel away from/towards the 
        // center point, per revolution.
        let radiusPerRevolution = (endRadius - startRadius) / totalAngle

        let action = SKAction.customActionWithDuration(duration) { node, time in
            // The current angle the node is at.
            let θ = totalAngle * time / CGFloat(duration)

            // The equation, r = a + bθ
            let radius = startRadius + radiusPerRevolution * θ

            node.position = pointOnCircle(angle: θ, radius: radius, center: centerPoint)
        }

        return action
    }
}

      

Finally, a usage example. In didMoveToView

:

let node = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 10, height: 10))
node.position = CGPoint(x: size.width / 2, y: size.height / 2)
addChild(node)

let spiral = SKAction.spiral(startRadius: size.width / 2,
                             endRadius: 0,
                             angle: CGFloat(M_PI) * 2,
                             centerPoint: node.position,
                             duration: 5.0)

node.runAction(spiral)

      

+4


source


While the above solution is brilliantly thought, there is a much easier way.



  • Add a spinnerNode and re-rotate it using rotateBy
  • Add your sprite as a child to spinnerNode (your sprite will by default rotate with spinnerNode)
  • Move your spriteNode (with moveTo) to the center of the wheelNode (your sprite will move in a spiral path towards the center).
+1


source







All Articles