What algorithm is available to correlate a SKSpriteNode's position with the UIBezierPath's currentPoint?
I have this code (GameViewController
) which implements the following of a SKSpriteNode
along a UIBezierPath
:
func createTrainPath() { trackRect = CGRect(x: tracksPosX - tracksWidth/2, y: tracksPosY, width: tracksWidth, height: tracksHeight) trainPath = UIBezierPath(ovalIn: trackRect)} // createTrainPathfunc startFollowTrainPath() { var trainAction = SKAction.follow( trainPath.cgPath, asOffset: false, orientToPath: true, speed: theSpeed) trainAction = SKAction.repeatForever(trainAction) myTrain.run(trainAction, withKey: runTrainKey)} // startFollowTrainPathfunc stopFollowTrainPath() { guard myTrain == nil else { myTrain.removeAction(forKey: runTrainKey) savedTrainPosition = getPositionFor(myTrain, orPath: trainPath) return }} // stopFollowTrainPath
The user of this segment can bring up another SKScene
.. at which action I call stopFollowTrainPath()
and save the position
of the SKSpriteNode
to the global savedTrainPosition
via:
func getPositionFor(_ node: SKSpriteNode, orPath: UIBezierPath) -> CGPoint { if (useNode) { return node.position } else { return orPath.currentPoint }} // getPositionFor
(1) When the user returns from looking at the other SKScene
, I want the moving (following) SKSpriteNode
to continue moving around the UIBezierPath
starting where I stopped the Node
before presenting the other SKScene
.
(2) If the SKSpriteNode
is not moving before the other SKScene
is brought up, I need the position
of the SKSpriteNode
to remain where it was before the new SKScene
was presented.
What is the best method for doing these?