我是編程的新手,請大家幫忙。我試圖在整個遊戲中設置間隔來創建各種Sprite節點(使用更新功能)。但是,每個Sprite節點需要以不同的間隔產生。我有一堆「if」語句,它似乎導致我的幀速率下降,並導致應用程序滯後。有人能告訴我如何解決這個問題嗎?如果可能的話,還請向我解釋爲什麼這樣做,以便我能更好地理解編碼過程。謝謝!具有多個間隔的更新功能(SWIFT)
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()
}
不客氣。請保持我們/我的信息是否有幫助:-) – pbodsk