1

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")    
        }
P.Dane
  • 91
  • 8
  • The action: SKAction.moveTo is not effect by gravity, to move the bomb, you should use an impulse force instead. Check [this](https://developer.apple.com/reference/spritekit/skphysicsbody/1519900-applyimpulse) – Simone Pistecchia Feb 17 '17 at 16:02

1 Answers1

1

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)
    }
 }
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160