Any way to speed up gameplay gradually in Swift?

爱⌒轻易说出口 提交于 2019-12-10 21:17:12

问题


I'm currently working on a game using Spritekit. The game has objects which spawn at the top of the screen and fall towards the player character, and the game ends when the player character collides with any of the objects. I am trying to find a way to gradually speed up gameplay over time to make the game more difficult (i.e. objects fall at normal speed when the game begins, after 5 seconds speed up 50%, after 5 more seconds speed up another 50%, ad infinitum.)

Would I need to use NSTimer to make a countdown to increase the gravity applied to the falling objects? Sorry if this is a basic thing, I'm kind of new to programming.

Thanks, Jake

EDIT:

My spawn method for enemies-

let spawn = SKAction.runBlock({() in self.spawnEnemy()})
let delay = SKAction.waitForDuration(NSTimeInterval(2.0))
let spawnThenDelay = SKAction.sequence([spawn, delay])
let spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)
self.runAction(spawnThenDelayForever)

And my method for making the enemies fall-

func spawnEnemy() {
    let enemy = SKNode()
    let x = arc4random()
    fallSprite.physicsBody = SKPhysicsBody(rectangleOfSize: fallSprite.size)
    fallSprite.physicsBody.dynamic = true
    self.physicsWorld.gravity = CGVectorMake(0.0, -0.50)
    enemy.addChild(fallSprite)
}

回答1:


In spawnEnemy(), you set self.physicsWorld.gravity. Move this line to your update: method.

If you are not keeping track of the game's duration right now, you will want to implement that. You can use the parameter of the update: method to accomplish this.

You can then use the game duration to change the gravity.

For example,

override func update(currentTime: CFTimeInterval) {
    if gameState == Playing{
        //update "duration" using "currentTime"
        self.physicsWorld.physicsBody = CGVectorMake(0.0, -0.50 * (duration / 10.0))
    }
}

10.0 can be changed depending on how fast you want the gravity to increase. A higher number makes it change less drastically, and a smaller number makes the gravity increase quite rapidly.

Hopefully this answers your question.



来源:https://stackoverflow.com/questions/27679189/any-way-to-speed-up-gameplay-gradually-in-swift

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