具有多个间隔的更新功能(SWIFT)

时间:2015-06-17 07:38:25

标签: swift sprite intervals

我是编程的新手所以请帮忙。我正在尝试设置间隔以在整个游戏中创建各种Sprite节点(使用更新功能)。但是,每个Sprite节点都需要以不同的间隔生成。我有一堆“if”语句,它似乎导致我的帧速率下降并导致应用程序滞后。有人可以告诉我如何解决这个问题吗?如果可能的话,请向我解释为什么要这样做,这样我才能更好地理解编码过程。谢谢!

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 个答案:

答案 0 :(得分:2)

也许这不是正确的解决方案,但它至少可以简化事情。

需要考虑的几件事情。

  • 现在,您在每个timeBetweenBirds循环中生成一个新的update值,但如果您最终在if语句中,则只会使用它/ p>

    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
    }
    

正如我所说,我不知道这是否会加快速度,但它会简化您的代码,这总是很好: - )

答案 1 :(得分: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)
}

在这种特殊情况下,我每3帧检查一次加速度计。在你的情况下:

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太接近于零,或者你可以获得飞到各处的鸟类的机枪效果;偶尔发生在我身上。