gpt4 book ai didi

java - Libgdx 按钮 onclick 不起作用

转载 作者:行者123 更新时间:2023-12-02 02:48:50 28 4
gpt4 key购买 nike

我创建了一个按钮,我想在悬停和单击时更改其外观。我没有收到任何错误,但它不起作用。单击或悬停时不会更改图像。显示的唯一图像是来自 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();
}
}

最佳答案

在初始化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"));
...
...
}

关于java - Libgdx 按钮 onclick 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44140350/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com