Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Swift SpriteKit) Rotate sprite in the direction of touch

On the screenshot provided, the red arrow and cross are just for demonstration purposes and are not in the game. I would like the sprite of the spaceship to face the direction of the ball it shoots.

Link to image

Here is my current code for touch location

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    for touch in touches {
        let location = touch.locationInNode(self)

        var bullet = SKSpriteNode(imageNamed: "bullet")
        bullet.position = cannon.position
        bullet.size = CGSize(width: 35, height: 35)

        //physics
        bullet.physicsBody = SKPhysicsBody(circleOfRadius: bullet.size.width/2)
        bullet.physicsBody?.categoryBitMask = PhysicsCategory.bullet
        bullet.physicsBody?.collisionBitMask = PhysicsCategory.enemy
        bullet.physicsBody?.contactTestBitMask = PhysicsCategory.enemy
        bullet.name = "bullet"
        bullet.physicsBody?.affectedByGravity = false

        self.addChild(bullet)

        var dx = CGFloat(location.x - cannon.position.x)
        var dy = CGFloat(location.y - cannon.position.y)

        let magnitude = sqrt(dx * dx + dy * dy)

        dx /= magnitude
        dy /= magnitude

        let vector = CGVector(dx: 120.0 * dx, dy: 120.0 * dy) //adjust constant to increase impluse.

        bullet.physicsBody?.applyImpulse(vector)

        // I found this code bellow this comment, but it just moves the cannon's y position

        let direction = SKAction.moveTo(
            CGPointMake(
                400 * -cos(bullet.zRotation - CGFloat(M_PI_2)) + bullet.position.x,
                400 * -sin(bullet.zRotation - CGFloat(M_PI_2)) + bullet.position.y
            ),
            duration: 0.8)

        cannon.runAction(direction)
    }
}
like image 286
james Avatar asked Jun 24 '16 15:06

james


People also ask

What is a group action in SpriteKit?

Note: SpriteKit supports sequential and grouped actions. A sequential action means each specified action runs one after the other (sequentially). Sometimes you may want multiple actions to run at the same time. This can be accomplished by specifying a group action where all the actions specified run in parallel.

How do I animate a bear in Swift 3?

Create efficient animations with texture atlases. Change the direction the bear faces based on where it’s moving. Make your animated bear move in response to touch events. This tutorial assumes you know the basics of SpriteKit. If not, you might want to start with the SpriteKit Swift 3 Tutorial for Beginners. It’s time to get started.

What is a SpriteKit texture atlas?

Rather than eight separate bear images, your texture atlas will be one big image along with a file that specifies the boundaries between each individual bear image. SpriteKit is optimized to work with texture atlases. So using this approach can improve memory usage and rendering performance. It’s also nearly effortless.


1 Answers

Here is the code I found which works perfectly.

Rotate a sprite to sprite position not exact in SpriteKit with Swift

 let angle = atan2(location.y - cannon.position.y , location.x - cannon.position.x)
 cannon.zRotation = angle - CGFloat(M_PI_2)
like image 93
james Avatar answered Oct 06 '22 12:10

james