0

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

Peppo
  • 1,107
  • 1
  • 12
  • 19
Mathis Delaunay
  • 172
  • 1
  • 16
  • Welcome to StackOverflow! One way to accomplish what you are asking would be : http://stackoverflow.com/a/21793439/3402095 But that is not the only way. Search SO, there are few topics which alread cover this. About rotating node : http://stackoverflow.com/a/29443170/3402095 which is a nice solution, but if you are interested in something simpler, try to search SO again :) And finally, read this link to find out how to write a good question : http://stackoverflow.com/help/how-to-ask – Whirlwind Oct 19 '15 at 12:22
  • A simpler version of how to rotate node : http://stackoverflow.com/a/32418304/3402095 :D – Whirlwind Oct 19 '15 at 12:29
  • Thanks for help me :D I very happy to see that there is a good community on this site unlike the french community :p I go to check on the link you give me :) – Mathis Delaunay Oct 19 '15 at 13:17

1 Answers1

1

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)
        }
    }
Mathis Delaunay
  • 172
  • 1
  • 16