BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: SKSpriteNode to follow "complex" path?

Forums > Swift


Hi, How to achieve movement of the sprite with complex lines?

For example:

let path = UIBezierPath()
path.move(to: CGPoint(x: -160, y: 544))
path.addLine(to: CGPoint(x: 160, y: 256))
path.addLine(to: CGPoint(x: 224, y: 96))
path.addLine(to: CGPoint(x: -160, y: 0))
path.addLine(to: CGPoint(x: -122, y: -33))
path.addLine(to: CGPoint(x: 188, y: -125))
path.addLine(to: CGPoint(x: 0, y: -400))
    .follow(path.cgPath, asOffset: true, orientToPath: true, speed: 10)

but this not work at all... I wonder does anyone have similar problem?



I forgot to set initial position of enemy sprite to .zero, so the movment and path is ok, but they far from visible scene... So, sprite set to .zero, then .follow.


@twostraws  Site AdminHWS+

You will have more chance of getting help if you can clarify what you mean by it not working. Are you saying nothing moves, or it moves but doesn't move as you expect? Or something else?



Oh, yes, somtimes I forgot the main part... :( (assmed that is obvius)

All enemy sprites not moves at all.


@twostraws  Site AdminHWS+

Some things to check:

  1. Do you have more than one enemy on screen, and it's being applied to the wrong one?
  2. Do you have physics acting on the enemy, stopping it from moving?
  3. Does your scene have a sensible size?



It was number 3. Enemies path was way off screen, and in the update is removed everything that is out of safe rect... Beginners mistake.


This actually helped me as well. Using the move for a differerent reason, but helped clarify a good bit. Thanks for posting and thanks for putting up the answer.


Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.