Search code examples
swiftuibezierpathskspritenodebeziercurve

How to move SKSpriteNode in a curve without rotating the sprite in Swift


I'm trying to move a sprite with a curve. I got this code:

let path = UIBezierPath()
path.move(to: CGPoint.zero)
path.addQuadCurve(to: CGPoint(x: ball.position.x+200, y: ball.position.y+50), controlPoint: CGPoint(x: ball.position.x+100, y: ball.position.y+200))
    ball.run(SKAction.follow(path.cgPath, speed: 1.0))

I have few questions: 1 - why my sprite is rotating while moving, and if I can control this rotation? 2 - any idea why the ball is moving only small part of the way and very slow and not a smooth moving (10-20 seconds)?

Does anyone have any idea how this code works? All the answers I found were related to older Swift version that had different method.


Solution

  • At last I've found the solution :)

    func beizerSprite()
       {
        // create a bezier path that defines our curve
        let path = UIBezierPath()
        path.move(to: CGPoint(x: 16,y: 239))
        path.addCurve(to:CGPoint(x: 301, y: 239),
                      controlPoint1: CGPoint(x: 136, y: 373),
                      controlPoint2: CGPoint(x: 178, y: 110))
    
        // use the beizer path in an action
        _playButton.run(SKAction.follow(path.cgPath,
                                        asOffset: false,
                                        orientToPath: true,
                                        speed: 50.0))
       }
    

    This move the SKSprite in a curve along the screen.