R_C R_C - 7 months ago 244
Swift Question

Move Sprite Node While Rotating

I am attempting to move a sprite node (a frisbee) in Swift while rotating it. The object moves as expected, but does not rotate. I grouped the two actions together to run in parallel, but it still does not rotate. If I run both actions by themselves they work, but not in parallel. Here is the code:

//spin frisbee
let oneRevolution = SKAction.rotate(byAngle: CGFloat.pi * 2, duration: 0.5)
let repeatRotation = SKAction.repeatForever(oneRevolution)

//move frisbee in an arc

let path = UIBezierPath()
path.move(to: CGPoint.zero)
path.addQuadCurve(to: CGPoint(x: 3200, y: 1500), controlPoint: CGPoint(x: 50, y: 2000 ))
let moveFrisbee = SKAction.follow(path.cgPath, duration: 1.3)

let group = SKAction.group([repeatRotation, moveFrisbee])
let frisbeeSequence = SKAction.sequence([frisbeeSound, group])

Answer Source

It took me about 22 minutes to realize this, but the SKAction.follow function automatically sets the zRotation to rotate relative to the path it follows. I disabled it by changing the "let moveFrisbee" line of code to this:

let moveFrisbee = SKAction.follow(path.cgPath, asOffset: true, orientToPath: false, duration: 1.3)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download