按下按键逐渐加速精灵;关键释放逐渐减速 [英] Gradually accelerate sprite on key pressed; gradually decelerate on key released

查看:126
本文介绍了按下按键逐渐加速精灵;关键释放逐渐减速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图弄清楚如何让逐渐加速按键上的精灵,然后一旦释放按键,就会慢慢减速到停止,就像小行星。如果可能的话,我希望没有任何游戏引擎。我在SO上进行了搜索,发现了相关的问题,但他们并没有在我的意见中正确回答我的问题。



我以为到目前为止:

  //当键被按下时
//通过增加y值移动
//但是继续增加金额y增加,因为你持有这个,
//直到达到一定的maxSpeed
//当钥匙被释放时,逐渐减速到零

我只是不确定如何正确编程,因为我只能想办法增加相同的价值,而不是逐渐加速,而持有。

所以这里是我的目标(逐渐加速,然后逐渐减慢)

>



我是初学者,对所有想法都开放,但我可能没有解决这个问题因为我不知道和其他人一样 解决方案

前面的问题几乎涵盖了这个主题,但如果你想完美的话,还有一件事你需要注意:非均匀的时间片,也就是不同的持续时间的蜱。通常情况下,你的tick()方法应该接受当前时间和最后一个tick的持续时间作为参数。 (如果没有,那么你需要通过查询当前时间来计算最后一个滴答的持续时间,并记住上次滴答发生的时间,这样你就可以从另一个中减去一个滴答。)



因此,在每个时间点上,您不应该简单地将当前速度添加到您的位置;您应该在每个时间点上做什么,将当前速度乘以最后一个时间点的持续时间加到您的头寸上。

这样,如果一个tick发生得非常快,另一个tick需要很长时间才能完成,那么你的飞船的移动仍然是一致的。 b $ b

I've been trying to figure out how I can gradually accelerate a sprite on key pressed and then once the key is released, gradually slow down to a stop, much like the ship in Asteroids. I would like to do this without any game engine if possible. I searched this up on SO and found related questions, but they did not answer my question exactly in my opinion.

What I've thought of so far is:

//while the key is being pressed
//move by increasing y value
//but continue to increase the amount y increases by as you hold this down,
//until it reaches certain maxSpeed
//when the key is released, gradually decelerate to zero

I'm just unsure as to how to properly program this, because I can only think of ways to just increase by the same value and not gradually accelerate while holding.

So here is my goal (Gradually speed up and then gradually slow down):

I am a beginner and am open to all ideas, but the fact that I may not be approaching this problem correctly could be because I do not know as much as others

解决方案

The previous questions have pretty much covered the subject, but if you want perfection, there is one more thing you need to be aware of: non-uniform time slices, that is, ticks of varying durations.

Normally, your tick() method should accept the current time and the duration of the last tick as parameters. (If it does not, then you need to calculate the duration of the last tick by querying the current time and remembering the time at which the last tick occurred, so that you can subtract one from the other.)

So, on each tick, you should not simply add the current speed to your position; what you should do instead on each tick, is add the current speed multiplied by the duration of the last tick to your position.

This way, if one tick happens very fast, and another tick takes a long time to complete, the movement of your spaceship will still be uniform.

这篇关于按下按键逐渐加速精灵;关键释放逐渐减速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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