Mathis Delaunay
Mathis Delaunay

Reputation: 191

Move my SpriteNode Swift SpriteKit

Sorry for my english I am French. Hello everyone.

I am currently in the process of programming a little game in Swift, and I wanted to know how to move my character is in the middle at the start, and to move when using the function to touchesMoved him follow my finger but not instantly , it is its own speed can say . And if so do you also can tell me how to make my character ( eg A arrow ) in the same direction where I put my finger when I drag my finger on the other side of the arrow screen changes direction :)

Thank you for the help

Upvotes: 0

Views: 927

Answers (1)

Mathis Delaunay
Mathis Delaunay

Reputation: 191

That's perferctly work , this is my code :

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch : AnyObject in touches {
            let location = touch.locationInNode(self)

            let actionBouger = SKAction.moveTo(CGPoint(x: location.x, y: location.y), duration: 2.5)
            Vaisseau.runAction(actionBouger)

            let dx = location.x - Vaisseau.position.x
            let dy = location.y - Vaisseau.position.y
            var angleInRadians = atan2(dy, dx) - CGFloat(M_PI_2)

            if(angleInRadians < 0){
                angleInRadians = angleInRadians + 2 * CGFloat(M_PI)
            }

            Vaisseau.zRotation = angleInRadians

            let actionAngle = SKAction.rotateToAngle(angleInRadians, duration: 0)
            Vaisseau.runAction(actionAngle)
        }
    }

Upvotes: 1

Related Questions