3

I'm in the process of updating my current app, and I was wondering if there is a way to rotate a sprite node around the center? For example, Earth revolving around the Sun. I'm also using the sks file.

This is what I have:

import SpriteKit
import GameplayKit

class StartScene: SKScene  {

var singleTap = SKSpriteNode()

var path = UIBezierPath()

var gameStarted = Bool()

override func didMove(to view: SKView) {

    singleTap = self.childNode(withName: "single") as! SKSpriteNode
    singleTap.anchorPoint = CGPoint(x: 0.5, y: 0.5)

    moveCounterClockwise()


}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


}



 func moveCounterClockwise() {




    let dx = singleTap.position.x - self.frame.midX
    let dy = singleTap.position.y - self.frame.midY

    let rad = atan2(dy, dx)


    path = UIBezierPath(arcCenter: CGPoint(x: self.frame.midX, y: self.frame.midY), radius: 120, startAngle: rad, endAngle: rad + CGFloat(Double.pi * 4), clockwise: true)

    let follow = SKAction.follow(path.cgPath, asOffset: false, orientToPath: true, speed: 60)

    singleTap.run(SKAction.repeatForever(follow))

}


override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered



   }
}

This code makes the sprite rotate around the center, but it also rotates the sprite itself by 90 degrees.

I have also looked at this answer, but I can't wrap my head around how to write it: https://stackoverflow.com/a/19045698/3926691

Edit: These two images are based on what I'm trying to accomplish and what is happening. It seems like the UIBezierPath is causing some sort of unwanted Z-Rotation.

enter image description here enter image description here

Any help is welcome, and I can try to provide clarity if needed.

behind the aura
  • 296
  • 2
  • 14
  • Correct me if I'm wrong, your sprite follows a path, which revolves around the center but you don't like the fact that it or rotates, or "spinning " by 90 degrees? – sicvayne Jun 18 '17 at 22:56
  • @sicvayne The sprite's angle slightly turns once the code activates. I've updated the op with an image for how I'm trying to achieve this. – behind the aura Jun 19 '17 at 01:25
  • would you be able to post some code for the image you are using? – sicvayne Jun 19 '17 at 01:37
  • @sicvayne Do you mean image I just added to the op? That image came from https://stackoverflow.com/q/19045067/3926691 . That user was in the same boat as I'm in, and I'm just using their image as reference – behind the aura Jun 19 '17 at 01:42
  • I mean as in does your sprite have a physicsBody and how you set it up. – sicvayne Jun 19 '17 at 01:50
  • @sicvayne The sprite doesn't have a physicsBody. I didn't think that it I need to add it. Do I need to? – behind the aura Jun 19 '17 at 01:53
  • you can try it. Set allowsRotation to false. – sicvayne Jun 19 '17 at 02:20

1 Answers1

1

If you don't want your sprite to rotate whilst following the path you have defined for it, set orientToPath to false.

Change:

let follow = SKAction.follow(path.cgPath, asOffset: false, orientToPath: true, speed: 60)

to

let follow = SKAction.follow(path.cgPath, asOffset: false, orientToPath: false, speed: 60)

(Try let follow = SKAction.follow(path.cgPath, speed: 60) first, as asOffset and orientToPath default to false, I think)

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
Steve Ives
  • 7,894
  • 3
  • 24
  • 55
  • I actually do need the orientToPath to be set to true. The sprite seems to change it's original upright angle by 90 or so degrees, and move to the right and down by 10 points. I can tell that the UIBezierPath is causing it, but I don't know how to fix it. I have edited my question with images. Any idea? – behind the aura Jun 21 '17 at 01:16
  • The change in the sptire’s position could be due to the fact that the sprite initially is not on the path. Then, when you run the ‘follow’ action, the sprite will move to the path. Make sure that the start of the path is the same as the sprite’s position. The rotation issue is that the sprite’s 0 rotation will continuously align with the path - do you want the sprite to only face in one of the 4 directions up, down, left or right? – Steve Ives Jun 21 '17 at 06:25