使用libgdx加快游戏速度 [英] Speed in game with libgdx
问题描述
我正在使用libgdx进行游戏.目前,每个角色都有一个速度,该速度实际上对应于更新角色之前游戏等待的渲染次数.例如,如果角色的速度为15,则将每15个渲染器更新一次.我意识到这不是必须要做的.
I'm making a game using libgdx. For now, every character has a speed, corresponding actually to the number of render the game wait before update the character. For example, if the character has a speed of 15, it will be updated every 15 renders. I'm conscious that this is not how it has to be done.
执行此操作的正确方法是什么?我真的想以%速度,例如,一个字符的速度为85%.
What is the proper way to do this? I really want to make a speed in %, for example a character will have a speed of 85%.
推荐答案
使用delta
.
Gdx.graphics.getDeltaTime()
方法返回自上一个渲染帧以来的秒数.通常,此值非常小,等于1/FPS.
Gdx.graphics.getDeltaTime()
method return secods since last render frame. Usually this value is very small, and it equal 1 / FPS.
@Override
public void render()
{
// limit it with 1/60 sec
float dt = Math.min(Gdx.graphics.getDeltaTime(), 1 / 60f);
// then move your characted according to dt
player.pos = player.pos + player.speed * dt;
// or, you could mute the speed like this:
player.pos = player.pos + player.speed * dt * 0.85;
}
这篇关于使用libgdx加快游戏速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!