我创建了一个按钮,我希望在悬停和点击时更改其外观。我没有错误,但它没有工作。单击图像或悬停图像时,图像不会更改。显示的唯一图片是来自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();
}
}
答案 0 :(得分:2)
在初始化InputProcessor
之后设置Stage
,如下所示:
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"));
...
...
}