Delta value in render method libgdx

后端 未结 2 1889
无人共我
无人共我 2021-01-12 11:27

I have check delta value in render method in Screen class.. I saw it is not constant. Can any body tell where it come from and what it is? And does it differ in different sc

2条回答
  •  攒了一身酷
    2021-01-12 12:04

    The deltaTime has nothing to do with screen sizes. It is the amount of time the last frame took to be rendered. Rendered in case of LibGDX also includes all the logic you execute in your render() method.

    Usually you want a game to run at the same speed on different devices. If your render method looks something like this...

    public void render(float deltaTime) {
        float speed = 5f;
        position = position + speed;
    }
    

    ...then the position would change faster on fast devices and less fast on slow devices, just because the render method is called a different amount of times.

    To overcome this issue, there is the deltaTime which in case of LibGDX is in seconds. To let the position change at the same rate on different devices, you usually do it like this:

    public void render(float deltaTime) {
        float speedPerSecond = 5f;
        position = position + (speedPerSecond * deltaTime);
    }
    

提交回复
热议问题