update function w/ multiple intervals (SWIFT)

I'm a newbie to programming so please help. I'm trying to set up intervals to create various Sprite nodes throughout my game (using the update function). However, each Sprite node needs to spawn at different intervals. I have a bunch of "if" statements and it seems to be causing my frame rate to drop and cause the app to lag. Can someone please tell me how to fix this? If possible, please also explain to me why it does this so that I can better understand the coding process. Thank you!


<code>


var timeBetweenBirds: Int!

var timeBetweenBadBirds: Int!

var timeBetweenSubtractTimeBirds: Int!

var timeBetweenAddTimeBirds: Int!

var timeBetweenBonusBirds: Int!


var now : NSDate?

var nextTime : NSDate?

var badBirdNextTime: NSDate?

var subtractTimeBirdNextTime: NSDate?

var addTimeBirdNextTime: NSDate?

var bonusBirdNextTime: NSDate?


override func update(currentTime: CFTimeInterval) {


timeBetweenBirds = Int(arc4random_uniform(3))

timeBetweenBadBirds = Int(arc4random_uniform(3) + 2)

timeBetweenSubtractTimeBirds = Int(arc4random_uniform(3) + 2)

timeBetweenAddTimeBirds = Int(arc4random_uniform(1) + 5)

timeBetweenBonusBirds = Int(arc4random_uniform(20) + 20)


now = NSDate()


if now?.timeIntervalSince1970 > nextTime?.timeIntervalSince1970 {

nextTime = now?.dateByAddingTimeInterval(NSTimeInterval(timeBetweenBirds))

createBird()

}


if now?.timeIntervalSince1970 > badBirdNextTime?.timeIntervalSince1970 {

badBirdNextTime = now?.dateByAddingTimeInterval(NSTimeInterval(timeBetweenBadBirds))

createBadBird()

}


if now?.timeIntervalSince1970 > subtractTimeBirdNextTime?.timeIntervalSince1970 {

subtractTimeBirdNextTime = now?.dateByAddingTimeInterval(NSTimeInterval(timeBetweenSubtractTimeBirds)) createSubtractTimeBird()

}


if now?.timeIntervalSince1970 > addTimeBirdNextTime?.timeIntervalSince1970 {

addTimeBirdNextTime = now?.dateByAddingTimeInterval(NSTimeInterval(timeBetweenAddTimeBirds))

createAddTimeBird()

}


if now?.timeIntervalSince1970 > bonusBirdNextTime?.timeIntervalSince1970 {

bonusBirdNextTime = now?.dateByAddingTimeInterval(NSTimeInterval(timeBetweenBonusBirds))

createBonusBird()

}


</code>

I've always found that using NSDate objects can be a little costly...


Instead of creating a new NSDate() object every frame, can't you just use currentTime which is provided by the function to check spawning times?

It says in the documentation:


"

currentTime
The current system time."


I don't exactly know how to use currentTime, but I figure this is probably the best way to solving your problem.

curentTime
is the current time in seconds (since 1/1/1970, probably) as a
double
which you can use directly. No need to convert everything to
NSDate
s. I usually have a scene property like
lastUpdateTime
which I initialize to -1. Then I have code at the top of the
update
method like


if lastUpdateTime == -1 {
 lastUpdateTime = currentTime
}
let deltaTime = currentTime - lastUpdateTime
update function w/ multiple intervals (SWIFT)
 
 
Q