increase game speed and keep same distance between nodes

北城余情 提交于 2019-12-24 19:47:15

问题


I am working on a game where I have two functions . The first function is used to create a row of obstacles. The gameSpeed variable is used to decide how frequently a new row of obstacles is added. Example : if I set gameSpeed to 3 then a new obstacle row is added every 3 seconds.

func addobstalce() {
    lastYieldTimeInterval += timeSinceLastUpdate
    if lastYieldTimeInterval > gameSpeed {
        lastYieldTimeInterval = 0
        nextRow(cR: currentRow)
    }
}

and the second function is used to add movement to them. this function has a variable called travelSpeed. I have set travelSpeed to 8 right now.

func addMovement (obstacle : SKSpriteNode) {
    let move = SKAction.move(to: CGPoint(x: obstacle.position.x, y: self.size.height/1.5), duration: TimeInterval(travelSpeed))
    let remove = SKAction.removeFromParent()
    let both = [move,remove]
    moveAndRemove = SKAction.sequence(both)
    obstacle.run(moveAndRemove)
}

so when above code is executed a new obstacle is added every three seconds and that obstacle takes 8 seconds to go from the bottom of screen to top of screen.

but as my game progresses I want to increase the speed of the whole game. I can change travelSpeed and gameSpeed variables but when I do so the distance between two obstacles does not remain same. How should I increase speed of my game while keeping the distance between two obstacles same.

I am using a switch statement to regulate the travelSpeed and gameSpeed variables but I am willing to use other methods as well.

switch obstaclesMade {
    case 10:
        //  increase speed by changing travelSpeed and/or gameSpeed
    case 20:
        //  increase speed by changing travelSpeed and/or gameSpeed
    case 30:
        //  increase speed by changing travelSpeed and/or gameSpeed
    default:
        break
}

Here is the function that adds obstacles

        func addRow (type :rowType){
        let obst1 = addObstacle(type: .small)
        let obst2 = addObstacle(type: .small)
        let obst3 = addObstacle(type: .small)
        obst1.position = CGPoint(x: self.frame.minX + obst1.size.width/2, y: obst1.position.y)
        obst2.position = CGPoint(x: self.frame.midX, y: obst1.position.y)
        obst3.position = CGPoint(x: self.frame.maxX - obst1.size.width/2, y: obst1.position.y)
        addChild(obst1)
        addChild(obst2)
        addChild(obst3)
        addMovement(obstacle: obst1)
        addMovement(obstacle: obst2)
        addMovement(obstacle: obst3) }

Thanks for your help.

来源:https://stackoverflow.com/questions/52443919/increase-game-speed-and-keep-same-distance-between-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!