有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

单击几下libgdx后java按钮没有响应

我似乎找不到这个问题的答案。为什么我的开始按钮在点击一定次数后会冻结(3)?它在前3次点击中起作用,然后决定停止工作。我仍然可以看到屏幕正在响应(按钮在单击时变为红色),但屏幕上的文本没有改变。这就像单击侦听器停止响应一样

public class MainMenu implements Screen{

    private Game game;
    private Stage stage;

    private TextButton Start_btn;
    private TextButton LocalWifi_btn;
    private TextButton Internet_btn;

    private TextButton Settings_btn;


    private boolean Start_clicked = false;
    private boolean LocalWifi_clicked = false;
    private boolean Internet_clicked = false;
    private boolean Settings_clicked = false;


    public MainMenu(Game g){
        game = g; //The Wasteland

        stage = new Stage(new ExtendViewport(Gdx.graphics.getWidth(),
                Gdx.graphics.getHeight())); //create a new stage with viewport to draw 2d stuff on

        Gdx.input.setInputProcessor(stage); //all input set to stage


        Skin skin = new Skin(Gdx.files.internal("gui/uiskin.json"), new TextureAtlas(Gdx.files.internal("gui/uiskin.atlas"))); //need this before you can make a gui

        Start_btn = new TextButton("" + Start_clicked, skin);

        Start_btn.setPosition(500, 500);
        Start_btn.setSize(200, 200);
        Start_btn.getLabel().setFontScale(10, 10); //change text size

        Start_btn.addListener(new ClickListener(){
            @Override
            public void touchUp(InputEvent e, float x, float y, int point, int button){
                onStartClicked();
            }
        });
        stage.addActor(Start_btn);

    }

    private void onStartClicked(){

        if(!Start_clicked){
            Start_clicked = true;
            Start_btn.setText("" + Start_clicked);
            Gdx.app.log("Button", "" + Start_clicked);

        }
        else{
            Start_clicked = false;
            Start_btn.setText("" + Start_clicked);
            Gdx.app.log("Button", "" + Start_clicked);

        }

    }

    @Override
    public void render(float delta) {

        //this has to be before anything or else it will be drawn on top of everything else
        Gdx.gl.glClearColor(0, 0, 0, 1); //set background color
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); //clears the screen

        stage.act(delta); //send ammount of time since last render call, tells it to keep a steady fps

        stage.draw();

    }

    @Override
    public void resize(int width, int height) {
        // use true here to center the camera
        // that's what you probably want in case of a UI
        stage.getViewport().update(width, height, true);


    }

}

我做错了什么


共 (1) 个答案

  1. # 1 楼答案

    不要在不调用super.touchUp()的情况下重写touchUp(),因为它会破坏ClickListener的功能。但这并不是你想要覆盖的。您应该覆盖clicked(),因此只有在您仍在按钮上方释放单击时才会触发。或者更好的是,使用ChangeListener。按钮已经内置了ClickListener,在单击按钮时触发更改事件。当您可以只使用ChangeListener时,添加另一个ClickListener是多余的