1

I'm creating a game, and I have a canon that shoots bullets. I have already the code to change the orientation of the canon, :

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {


    for touch: AnyObject in touches {


        let location = touch.locationInNode(self)


        if self.nodeAtPoint(location) == self {


            if location.x < self.size.width / 2 {


                let x = location.x
                let y = location.y

                let AB = self.size.width / 2 - x

                let angleRadians = atan(AB/y)

                let angleDegrees = angleRadians * (180 / CGFloat(M_PI))

                canon.zRotation = angleDegrees * (CGFloat(M_PI) / 180)


            }


            if location.x > self.size.width / 2 {


                let x = location.x
                let y = location.y

                let AB = x - self.size.width / 2

                let angleRadians = atan(AB/y)

                let angleDegrees = angleRadians * (180 / CGFloat(M_PI))

                canon.zRotation = -(angleDegrees * (CGFloat(M_PI) / 180))


            }


        }


    }

and I want to determine the direction of the bullet when the user shoots, I tried with an action (moveBy) but I didn't find a relation between the orientation of the canon, and the vector of the bullet's direction. If the location of the touch is CGPoint(x : 100, y : 100), the vector cannot be CGVectorMake(100, 100), because it is too big values !

I tested with a moveTo action, the direction of the bullet was perfect, but the bullet stopped on the touch location, and I want that the bullet continue to move forever !

Thanks for your help !

weston
  • 54,145
  • 21
  • 145
  • 203
fredericdnd
  • 968
  • 1
  • 14
  • 25
  • This should be what you're looking for [link](http://stackoverflow.com/questions/26364921/sprite-kit-apply-impulse-to-shoot-projectile-at-character) – 0x141E Mar 07 '15 at 20:33
  • I didn't understand, because he use the launcher position and the bird, while I use the launcher (canon) and a projectile (ball). And he use Obj-C, not Swift ! – fredericdnd Mar 07 '15 at 21:36
  • don't you want the cannon ball to be affected by gravity? What is requirement exactly? Do you just want to shoot the projectile in the direction the cannon is oriented? – rakeshbs Mar 09 '15 at 06:53

0 Answers0