如何防止节点加速太多?

时间:2016-02-08 19:17:23

标签: swift sprite-kit

我的节点从我的场景顶部落下并且设置为加速如此:

   var droptime: NSTimeInterval = 20.5

 class GamePlayScene: SKScene, SKPhysicsContactDelegate {
       droptime = 20.5

    runAction(SKAction.repeatActionForever(
        SKAction.sequence([
            SKAction.runBlock(array),
            SKAction.waitForDuration(1.6)])))





  func array()  {

  let colorCount = 5

    let index=Int(arc4random_uniform(UInt32(colorCount)))

    let dots = SKSpriteNode(imageNamed: "Color\(index+1)")
    dots.position = CGPointMake(150, 600)
    dots.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(45, 45))
    dots.physicsBody?.dynamic = true
    dots.physicsBody?.affectedByGravity = false
    for i in 0..<5 {
        dots.physicsBody?.categoryBitMask = UInt32(0x1 << index)
        dots.physicsBody?.contactTestBitMask = UInt32(0x1 << index)

    }
   addChild(dots)

             droptime -= 1.09
          dots.size = CGSizeMake(45, 45)
    dots.runAction(
        SKAction.moveByX(0, y: -1600,
            duration: NSTimeInterval(droptime)))


}

}

随着时间的推移,显然他们会继续加速并最终因游戏过快而下降。我想知道是否有一种方式,一旦他们达到一定的速度,他们将保持这样的速度,以便他们不会以令人讨厌的速度快速下降,游戏变得无法播放。

2 个答案:

答案 0 :(得分:1)

您可以在停机时间设置max

let minInterval = 5.0 //min interval of 5 sec
//minInterval is your smallest time interval desired - that's how you can limit speed
droptime = max(droptime - 1.09, minInterval)  

答案 1 :(得分:0)

你可以有if个案例来检查丢弃时间,只有当它大于某个最小时间时才会减少它。

if droptime > minTime:
    droptime -= 1.09