2015-06-17 52 views
0

我是編程的新手,請大家幫忙。我試圖在整個遊戲中設置間隔來創建各種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() 

    } 

回答

2

也許這不是正確的解決方案,但它至少會簡化一些事情。

一些事情要考慮。

  • 現在你正在生成每個update環新timeBetweenBirds值,但你只使用它,如果你在if聲明

    override func update(currentTime: CFTimeInterval) { 
    
        timeBetweenBirds = Int(arc4random_uniform(3)) //Created on each update loop 
        ... 
        now = NSDate() 
    
        if now?.timeIntervalSince1970 > nextTime?.timeIntervalSince1970 { 
         //Only used here   
         nextTime = now?.dateByAddingTimeInterval(NSTimeInterval(timeBetweenBirds)) 
         createBird() 
        } 
    

    落得這樣一個開始的地方可能是僅在需要時更新timeBetweenBirds值。

  • 正如我所看到的,您不需要創建所有這些日期並在它們之間進行比較。

    每次update被調用時,你會得到一個currentTime屬性免費:-)這個屬性可以用來檢查針對nextTime變量,像這樣:

    var nextTime: CFTimeInterval = 0 
    ... 
    override func update(currentTime: CFTimeInterval) { 
        if currentTime > nextTime { 
         nextTime = currentTime + CFTimeInterval(arc4random_uniform(3)) 
         createBird() 
        } 
        //And so on with your other if statements 
    } 
    

正如我所說的,我不不知道這是否會加快速度,但它會簡化代碼,這總是很好:-)

+0

不客氣。請保持我們/我的信息是否有幫助:-) – pbodsk

0

我總是給每個函數更新調用自己的時間變量,以便它不必運行許多代碼塊每1/60的一個第二。 實施例:

var timeOfLastUpdateForUserMotion:CFTimeInterval = 0 

func processUserMotionForUpdate(currentTime: CFTimeInterval){ 
    if (currentTime - timeOfLastUpdateForUserMotion > 0.1) { 
     ... 
     timeOfLastUpdateForUserMotion = currentTime 
    } 
} 
override func update(currentTime: CFTimeInterval) { 
    processUserMotionForUpdate(currentTime) 
} 

在我檢查加速度計,每三幀一次這種特殊情況下。你的情況:

var timeOfLastUpdateForRandomVariable 
var birdOneInterval:Double = 0 
var birdTwoInterval:Double = 0 

func updateRandomVariables(currentTime:CFTimeInterval){ 
    if (currentTime - timeOfLastUpdateForRandomVariable > 1.0){ 
     birdOneInterval = Int(arc4random_uniform(3)+1) 
     birdTwoInterval = Int(arc4random_uniform(3) + 2) 
     timeOfLastUpdateForRandomVariable = currentTime 
    } 
} 
    override func update(currentTime: CFTimeInterval) { 
     updateRandomVariables(currentTime) 
    } 

確保不要讓時間間隔,birdOneInterval,例如,太接近零,或者你可以讓鳥兒到處飛的機槍效果;偶爾發生在我身上。