libgdx - 如何处理与时间相关的事件?

She*_*rSt 1 android libgdx

我开始在libgdx中开发游戏,我想知道以下情况的最佳实践是什么.我正在尝试做两件事:将菜单(精灵)移动到位,然后将相机平移到播放器精灵.我完成这些事情的想法是在render()函数中有一个'action_stack'ArrayList.ArrayList将包含"Action"实例.每个Action实例都有一个step()功能,它将被覆盖.在render()函数中,我将遍历action_stack并触发每个元素的step()函数.因此,要完成将菜单移动到位,我将创建该类:

public class MenuAnim1 implements Action {

    private int targetX;
    private int targetY;
    private Sprite menu;

    public MenuAnim1() {
        //set initial sprite and position
    }

    public Step() (
        //move this.menu towards targetX and targetY
            //draw the sprite
        //if not at target position, do nothing
        //if at target position, remove this object from action_stack
    }
}
Run Code Online (Sandbox Code Playgroud)

...并将一个实例放入action_stack:

MenuAnim1 menuAnim1 = new MenuAnim1();
action_stack.add(menuAnim1);
Run Code Online (Sandbox Code Playgroud)

对不起,如果我的Java不好,我不是很熟悉它.无论如何,我的问题是:这是不是很好的做法?人们通常做什么?有没有更好的方法来做我上面描述的内容?

Les*_*tat 5

我从未使用过Action,但你的想法很好.如果您希望它们与时间相关(因此fps独立),请确保使用自上一帧到目前的时间,也称为deltadeltaTime.你可以这样得到它:

Gdx.graphics.getDeltaTime();
Run Code Online (Sandbox Code Playgroud)

所以,为了让你的动作移动精灵,例如,向右移动,这将有助于:

speed = 10; //It will move 10 units per second.
delta = Gdx.graphics.getDeltaTime();
menu.translateX(speed*delta);
Run Code Online (Sandbox Code Playgroud)

(Sprite#translateX)