我能够绘制按钮,但是单击/“触摸”按钮时什么也没有发生,而且我不知道自己在做什么错。谁能帮忙吗?触摸时,不应将textButtonStyle更改为“ ToothButton”吗?我不应该为Android应用程序使用InputListener吗?

MainMenuButtons.java

public class MainMenuButtons extends Stage {

Stage buttons;
MMButton startButton, optionButton;

public MainMenuButtons(Viewport viewport) {

    startButton = new MMButton(634,550, "Start");
    optionButton = new MMButton(634,450, "Options");

    buttons = new Stage(viewport);

    buttons.addActor(startButton);
    buttons.addActor(optionButton);
    Gdx.input.setInputProcessor(this);
}


MMButton.java(主菜单按钮)

public class MMButton extends Actor{
    TextButton button;
    TextButton.TextButtonStyle textButtonStyle;
    BitmapFont font;
    Skin skin;
    TextureAtlas buttonAtlas;


    public MMButton(int x, int y, String name) {
        font = new BitmapFont();
        skin = new Skin();

        buttonAtlas = new TextureAtlas(Gdx.files.internal("menuButton.atlas"));
        skin.addRegions(buttonAtlas);

        textButtonStyle = new TextButton.TextButtonStyle();
        textButtonStyle.font = font;
        textButtonStyle.up = skin.newDrawable("ToothButtonUp");
        textButtonStyle.down = skin.newDrawable("ToothButton");

        button = new TextButton(name, textButtonStyle);
        button.setBounds(x, y, 246, 90);
        button.setTouchable(Touchable.enabled);


        button.addListener(new InputListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                System.out.println("down");
                return true;
            }
            public void touchUp(InputEvent event, float x, float y, int pointer, int button ) {
                super.touchUp( event, x, y, pointer, button );
            }
        });
    }


MainMenuScreen.java(对不起,我真的只想解决这个问题!)()

OrthoCamera是我上线的一门课,简化了相机的使用。

公共类MainMenuScreen扩展Screen {

private OrthoCamera camera;
MainMenuButtons buttons;

@Override
public void create() {
    camera = new OrthoCamera();
    buttons = new MainMenuButtons(new ScreenViewport());
}
@Override
public void update() {
    camera.update();
    buttons.draw();
}
@Override
public void render(SpriteBatch batch) {
    batch.setProjectionMatrix(camera.combined);
    buttons.draw();
}
@Override
public void resize(int width, int height) {
    camera.resize();
    buttons.getViewport().update(width,height,true);

}

最佳答案

您的问题出在MainMenuButtons类上。它分为两个阶段。一个(按钮)已添加了按钮actor,另一个(按钮)被设置为输入处理器。不好

有两种解决方案。

要么...

替换此行...

Gdx.input.setInputProcessor(this);


有了这个...

Gdx.input.setInputProcessor(buttons);


并且不要理会扩展阶段。

或者(如果您真的想延长舞台)...

完全摆脱按钮变量,并替换这些行...

buttons.addActor(startButton);
buttons.addActor(optionButton);


有了这个...

addActor(startButton);
addActor(optionButton);

10-05 21:12