FREE: Start my 100 Days of SwiftUI today! >>

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 Bitrise

SPONSORED Build better iOS apps, faster. Looking for a great mobile CI/CD solution that has tons of iOS-specific tools, smooth code signing, and even real device testing? Learn more about Bitrise’s iOS-specific solutions.

Sign up for a free trial!

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.

Support my work on Patreon

 
Unknown user

Not logged in

Log in