P.Dane
P.Dane

Reputation: 91

How to make SKSpriteNode fall at a downward curve

What I have Now: The node currently only travels in a straight line on the x-axis.

let actionBomb = SKAction.repeatForever(SKAction.moveTo(x: 495, duration: 3.0))

What I’m looking to achieve: The goal is to have the node travel at a downward curve until hitting the ground. I’m not sure how to go about achieving this.

Here is my current code:

func bombAction(){


        if(self.action(forKey: "spawnBomb") != nil){return}
        let bombTimer = SKAction.wait(forDuration: 5, withRange: 8)
        let spawnbombA = SKAction.run {

            self.bomb = SKSpriteNode(imageNamed: "bomb1")
            self.bomb.zPosition = 1
            self.bomb.physicsBody = SKPhysicsBody(circleOfRadius: self.bomb.size.height / 10.0)
            self.bomb.physicsBody?.isDynamic = true
            self.bomb.physicsBody?.allowsRotation = false
            self.bomb.physicsBody?.affectedByGravity = true

            var bombPosition = Array<CGPoint>()

            bombPosition.append((CGPoint(x:400, y:475)))
            bombPosition.append((CGPoint(x:500, y:475)))
            bombPosition.append((CGPoint(x:750, y:475)))

 let shuffledLocations = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: bombPosition) as! [CGPoint]

            self.bomb.position = shuffledLocations[0]

            let spawnLocationBomb =  bombPosition[Int(arc4random_uniform(UInt32(bombPosition.count)))]

            let actionBomb = SKAction.repeatForever(SKAction.moveTo(x: 495, duration: 3.0))
            self.bomb.run(actionBomb)
            self.bomb.position = spawnLocationBomb
            self.addChild(self.bomb)
            print(spawnLocationBomb)

        }

       let sequenceBomb = SKAction.sequence([bombTimer, spawnbombA])
        self.run(SKAction.repeatForever(sequenceBomb), withKey: "spawnBomb")    
        }

Upvotes: 1

Views: 80

Answers (1)

Ashley Mills
Ashley Mills

Reputation: 53121

Don't forget to set gravity for your scene's physicsWorld

class GameScene: SKScene {

    override func didMove(to view: SKView) {        
        physicsWorld.gravity = CGVector(dx: 0, dy: -1)
    }
 }

Upvotes: 1

Related Questions