UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

3      

@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?

3      

@miff  

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

All enemy sprites not moves at all.

3      

@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?

4      

@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.

3      

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.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.