有什么方法可以在 Swift 中逐渐加快游戏速度?

编程入门 行业动态 更新时间:2024-10-11 19:22:20
本文介绍了有什么方法可以在 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 中逐渐加快游戏速度?

本文发布于:2023-10-26 17:31:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530843.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么   速度   方法   游戏   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!