如何使SKSpriteNode下降曲线

时间:2017-02-17 08:43:09

标签: swift xcode sprite-kit swift3 physics

我现在拥有的东西:节点目前只在x轴上沿直线行进。

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

我希望实现的目标:目标是让节点以向下的曲线行进,直到撞到地面。我不确定如何实现这一目标。

这是我目前的代码:

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")    
        }

1 个答案:

答案 0 :(得分:1)

不要忘记为场景的physicsWorld

设置重力
class GameScene: SKScene {

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