SALE: Save 50% on all my books and bundles >>

SOLVED: SKSpriteNode to follow "complex" path?

Forums > Swift

@miff  

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))

enemy.run(
    .follow(path.cgPath, asOffset: true, orientToPath: true, speed: 10)
)

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

Thanks

EDIT:

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 Admin

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?

   

@miff  

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

All enemy sprites not moves at all.

   

@twostraws  Site Admin

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?

1      

@miff  

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 RevenueCat

SPONSORED Building in-app subscriptions are hard. RevenueCat makes it simple. With their open source SDKs, you can painlessly implement subscriptions for your app in hours, not months.

Explore the docs to learn more

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Snapthread is a casual video editor and slideshow maker that makes discovering, compiling and sharing your favorite memories effortless.

 
Unknown user

Not logged in

Log in