KeepItSimple February 2016

How to prevent a node from speeding up too much?

I have nodes that fall from the top of my scene and are set to speed up like so:

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


}

}

And as time goes on, obviously they keep speeding up and eventually fall too fast for gameplay. I was wondering if there was a way that once they reach a certain speed, that they'll just stay at that speed so that they don't end up falling obnoxiously fast and the game becomes unplayable.

Answers


notevenwrong February 2016

You can have an if case that checks the droptime and decrements it only if it is greater than the some minimum time.

if droptime > minTime:
    droptime -= 1.09


Chris Slowik February 2016

You could put a max on the drop time.

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)  

Post Status

Asked in February 2016
Viewed 2,651 times
Voted 12
Answered 2 times

Search




Leave an answer