有什么方法可以在 Swift 中逐渐加快游戏速度? [英] Any way to speed up gameplay gradually in Swift?

查看:81
本文介绍了有什么方法可以在 Swift 中逐渐加快游戏速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Spritekit 开发一款游戏.游戏中的物体会在屏幕顶部生成并落向玩家角色,当玩家角色与任何物体发生碰撞时游戏结束.我试图找到一种方法随着时间的推移逐渐加快游戏速度,使游戏变得更加困难(即游戏开始时物体以正常速度下落,5 秒后加速 50%,再过 5 秒后加速 50%,无止境.)

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.)

我是否需要使用 NSTimer 进行倒计时以增加施加到下落物体上的重力?对不起,如果这是一个基本的东西,我是编程新手.

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.

谢谢,杰克

我的敌人生成方法-

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)
}

推荐答案

spawnEnemy() 中,您设置 self.physicsWorld.gravity.将此行移至您的 update: 方法.

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

如果您现在没有跟踪游戏的持续时间,您将需要实现它.您可以使用 update: 方法的参数来完成此操作.

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.

例如,

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 可以根据您希望重力增加的速度进行更改.数值越大,变化幅度越小,数值越小,重力增加越快.

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.

这篇关于有什么方法可以在 Swift 中逐渐加快游戏速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆