在 Xcode 中加速 NSTimer
我正在开发一个名为 MetorideStorm 的小应用程序。这是一个非常简单的游戏,其中一些墙壁需要每秒向下移动。现在游戏需要变得更加困难,墙壁需要更快地倒塌。我怎样才能做到这一点?我可以制作一个浮动并用它控制 NSTimer 吗?
以前,这款游戏是针对 iPhone 的,但我不知道这是否重要。我对 iPhone 和 iPad 编程非常陌生,所以我还不太了解。
谢谢 - 弗雷德里克·维特
I was working on my little app called MetorideStorm. It's a very simple game, where some walls need to move down, each second. Now the game needs to be more difficult, and the walls will need to fall down faster. How can i do that? Can i make a float, and control the NSTimer with that?
By the was, the game is for iPhone, but i don't know if that matter. I am very new to iPhone and iPad programming, so i don't know so much about it yet.
Thanks - Frederik Witte
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我要重新表述你的问题,我会这样说。我有一组墙,每堵墙都需要有一个基于难度的速度。在给定时间,每面墙的位置需要根据其速度进行更新。
一般来说,您不想改变此更新发生的时间,因为考虑到帧速率,运动可能会开始看起来不稳定。如果您看过《学习 iPhone 和 iPad cocos2d 游戏开发》一书,Steffen Itterheim 甚至建议始终假设恒定的帧速率。争论的焦点是,在正常游戏过程中,您确实需要恒定的帧速率。在帧速率可能下降的情况下(例如,有一封电子邮件传入,iPhone 必须进行额外的处理),您不希望任何运动跳得太远。话虽这么说,最好在这么短的时间内,事情进展缓慢,然后恢复到用户中断之前的位置。
因此,与其改变 NSTimer 的间隔,不如改变移动的距离。设置一个变量来保存壁的速度。将计时器设置为合理的值(1/60 是一个不错的选择)。然后,在计时器的回调中,根据速度更新墙的位置。
当然,这只是如果您正在滚动自己的游戏引擎的话。如果您使用的是 cocos2d 那么您应该真正使用 CCDirector 的调度程序,因为当导演暂停/恢复等时使用您自己的 NSTimer 会导致问题...如果您不使用 cocos2d 我建议您研究一下它,因为它会为你处理这类事情。
If I were to rephrase your question I would put it like this. I have a set of walls, each wall needs to have a velocity based on difficulty. At a given time, each wall's position needs to be updated based on its velocity.
In general you wouldn't want to vary the time at which this update takes place, as the movement could begin to look choppy, think frame rate. If you have a look at the book "Learn iPhone and iPad cocos2d Game Development" Steffen Itterheim even goes so far as to recommend always assuming a constant frame rate. The argument being, that during normal game play you really want a constant frame rate. In cases where the frame rate may drop (for example there is an incoming email and the iPhone has to do extra processing) you don't want any movement to jump too far. That being said, it would be better that for this short amount of time things move slowly and then resume where the user was before the disruption.
So rather than changing the interval of your NSTimer, it may be better to change the distance moved. Set up a variable that holds the velocity of the wall(s). Set up your timer with a sane value (1/60 is a good choice). Then in your callback from your timer update the position of the wall(s) based on the velocity.
Of course this is just if you are rolling your own game engine. If you are using cocos2d then you should really use the CCDirector's scheduler as using your own NSTimer will cause problems when the director is paused/resumed, etc... And if you aren't using cocos2d I'd recommend looking into it, as it takes care of this sort of thing for you.
嗯。如果您需要更改速度,请执行以下操作:
这就是 Cocos2d 的方式,因为您说您正在使用 Cocos2d。
Ummm. If you need to change the speed do this:
This is the way for Cocos2d since you said you were using Cocos2d.
如果你坚持“加速”计时器,当我查找它时,我发现做到这一点的唯一方法是使计时器无效并创建一个新计时器,不要认为还有其他方法......你唯一可以改变的事情是计时器使用 fireDate 属性,但这不会改变计时器触发之间的间隔,我认为它只会代表第一次时间计时器已触发或将触发。
If you insist on "speeding up" a timer, the only way i've found to do this when i looked it up was invalidating and creating a new one, don't think there's another way... The only thing you can change is the moment the timer will fire using the fireDate property, but this will not change the interval between timer fires, and i think it will only represent the first time the timer fired or will fire.