How to speed up a Sprite Kit game after every additional 50 points

前端 未结 2 1573
太阳男子
太阳男子 2020-12-21 13:45

I am making a 2D game with Sprite Kit and I need to add a method to speed up the game after every 50 points.

I worked out a method that adds the speed as wanted, bu

2条回答
  •  抹茶落季
    2020-12-21 14:34

    if you want to update the speed at regular intervals, you can use the modulus operator - take the remainder after dividing by your interval - if that equals zero, take action. The reason you find the game speeding up completely is that the next time through the update function, it's still on the right points level, and it just increments the speed - you could add another variable to track if you're ready for an increment

    // as part of class declaration
    var bReadyForIncrement : Bool = true
    let pointIncrement : Int = 4  // or whatever you need
    
    
    // inside the update method
    // points are added when an enemy is destroyed
    // pointSpeed is the float variable that is used to change positions
    
    if (points % 4) 
    {
        if bReadyForIncrement
        {
           pointSpeed++;
           bReadyForIncrement = false // set this to true wherever you add points
        }
    }
    

提交回复
热议问题