【问题标题】:Libgdx button onclick not workingLibgdx 按钮 onclick 不起作用
【发布时间】:2025-12-24 11:05:17
【问题描述】:

我创建了一个按钮,我想在悬停和单击时更改其外观。我没有收到任何错误,但它不起作用。单击或悬停时不会更改图像。唯一显示的图像是来自playButtonStyle.up 的图像。

这是我的代码:

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.starships.MainClass;
import com.sun.prism.paint.Color;
import helpers.Info;

public class MainMenuScreen implements Screen {

    MainClass game;
    Stage stage;    
    private Texture background;    
    private AssetManager assets;
    private TextureAtlas atlas;   
    private Skin skin;

    private Table table;
    private Button playButton;

    public MainMenuScreen(MainClass mainClass) {
        game = mainClass;

        Gdx.input.setInputProcessor(stage); 
        stage = new Stage();
        background = new Texture(Gdx.files.internal("Background.png"));

        assets = new AssetManager();
        assets.load("Buttons/PlayButtonAtlas.atlas", TextureAtlas.class);
        assets.finishLoading();
        atlas = assets.get("Buttons/PlayButtonAtlas.atlas");

        skin = new Skin(atlas);

        table = new Table(skin);
        table.setBounds(0, 0, Info.WIDTH, Info.HEIGHT);

        Button.ButtonStyle playButtonStyle = new Button.ButtonStyle();
        playButtonStyle.up = skin.getDrawable("PlayButton");
        playButtonStyle.over = skin.getDrawable("PlayButtonHover");
        playButtonStyle.down = skin.getDrawable("PlayButtonPressed");

        playButton = new Button(playButtonStyle);   
        table.add(playButton);
        stage.addActor(table);
    }

    @Override
    public void show() {

    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0.7f, 0.8f, 1);
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

        stage.act(delta);
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        stage.dispose();
    }
}

【问题讨论】:

标签: java button libgdx


【解决方案1】:

在初始化Stage 之后设置InputProcessor,如下所示:

public MainMenuScreen(MainClass mainClass) {
   game = mainClass;
   stage = new Stage();
   Gdx.input.setInputProcessor(stage);   // This call should be after initialisation of stage.
   background = new Texture(Gdx.files.internal("Background.png"));
   ...
   ...
}

【讨论】:

  • 谢谢。有效。我花了一整天的时间来制作一个按钮.. :\
  • @iulyus01 如果你想要多个输入处理器,你可以使用InputMultiplexer。将优先级较高的InputProcessors(阶段也是InputProcessor)放在第一位,然后添加InputMultiplexer,而不是像本答案中所示的阶段。