如何使LibGDX中的按钮独占

时间:2021-10-14 12:45:21

I'm making an app with a menu that utilizes a Stage and 3 Buttons. They all work well, the only thing that bothers me is that when I'm pressing a button I can also press the other ones(the animation still occurs). What can I do so that when I press a button the other ones are disabled?

我正在制作一个带有使用舞台和3个按钮的菜单的应用程序。它们都运作良好,唯一困扰我的是当我按下按钮时我也可以按其他按钮(动画仍然出现)。我可以做什么,当我按下按钮时,其他按钮被禁用?

1 个解决方案

#1


I created a subclass of Stage to fix this in my game. You can use a separate, standard Stage for non-UI stuff, and use this secondary stage just for the UI.

我创建了Stage的子类来修复我的游戏。您可以为非UI内容使用单独的标准舞台,并仅为UI使用此第二阶段。

public class SingleTouchStage extends Stage {

    public SingleTouchStage () {
        super();
    }

    public SingleTouchStage (Viewport viewport) {
        super(viewport);
    }

    public SingleTouchStage (Viewport viewport, Batch batch) {
        super(viewport, batch);
    }

    @Override
    public boolean touchDown (int screenX, int screenY, int pointer, int button) {
        if (pointer != 0)
            return false;
        return super.touchDown(screenX, screenY, pointer, button);
    }

    @Override
    public boolean touchDragged (int screenX, int screenY, int pointer) {
        if (pointer != 0)
            return false;
        return super.touchDragged(screenX, screenY, pointer);
    }

    @Override
    public boolean touchUp (int screenX, int screenY, int pointer, int button) {
        if (pointer != 0)
            return false;
        return super.touchUp(screenX, screenY, pointer, button);
    }
}

#1


I created a subclass of Stage to fix this in my game. You can use a separate, standard Stage for non-UI stuff, and use this secondary stage just for the UI.

我创建了Stage的子类来修复我的游戏。您可以为非UI内容使用单独的标准舞台,并仅为UI使用此第二阶段。

public class SingleTouchStage extends Stage {

    public SingleTouchStage () {
        super();
    }

    public SingleTouchStage (Viewport viewport) {
        super(viewport);
    }

    public SingleTouchStage (Viewport viewport, Batch batch) {
        super(viewport, batch);
    }

    @Override
    public boolean touchDown (int screenX, int screenY, int pointer, int button) {
        if (pointer != 0)
            return false;
        return super.touchDown(screenX, screenY, pointer, button);
    }

    @Override
    public boolean touchDragged (int screenX, int screenY, int pointer) {
        if (pointer != 0)
            return false;
        return super.touchDragged(screenX, screenY, pointer);
    }

    @Override
    public boolean touchUp (int screenX, int screenY, int pointer, int button) {
        if (pointer != 0)
            return false;
        return super.touchUp(screenX, screenY, pointer, button);
    }
}