Gradually accelerate sprite on key pressed; gradually decelerate on key released

后端 未结 4 1166
甜味超标
甜味超标 2020-12-21 11:16

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, muc

4条回答
  •  佛祖请我去吃肉
    2020-12-21 11:49

    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.

提交回复
热议问题