1

I am currently making an iPhone game using SpriteKit in which two archers fire arrows at each other in an attempt to take away all of their opponent's health. I've got the mechanism for firing the arrow when the user drags back on the screen, but am wondering if there is a way to make the arrow sprite rotate in mid air as it follows the arc (facing upwards immediately after release and facing downward immediately prior to landing) so that it actually looks as if it were fired from a bow in real life.

How it currently looks

This is the piece of code that is ran when the user's touch ends. You can see where I apply the impulse but I am not sure how to go about implementing the rotation. I understand how I could get the initial rotation to match the angle of the trajectory line using simple trigonometry, but don't know how to make it rotate with its arc.

func touchUp(atPoint pos : CGPoint) {

    if arrowFired == false && firstTouchMade == true && pointOne != nil {

        //Set pointTwo
        pointTwo = pos

        //Define the vert/hor of the pull-back
        let verticalLeg = ((pointOne?.y)! - (pointTwo?.y)!)
        let horizontalLeg = ((pointOne?.x)! - (pointTwo?.x)!)

        //Apply the impulse
        arrow?.run(SKAction.applyImpulse(CGVector(dx: horizontalLeg / 9, dy: verticalLeg / 9), duration: 0.001))
        arrow?.physicsBody?.affectedByGravity = true

        let array = [trajectoryLine, trajectoryLabel] as [Any]
        removeChildren(in: array as! [SKNode])

        arrowFired = true
    } else if firstTouchMade == false && zoomingIn == false {
        zoomingIn = true
        cameraNode.run(SKAction.move(to: (arrow?.position)!, duration: 1), completion: {
            self.firstTouchMade = true
            self.zoomingIn = false
        })
        cameraNode.run(SKAction.scale(by: 0.5, duration: 1))

    }
}

Sorry if this is an obvious question, I am relatively new to programming, Swift, and SpriteKit especially.

David Chopin
  • 2,780
  • 2
  • 19
  • 40

0 Answers0