繁体   English   中英

libgdx按钮onclick不起作用

[英]Libgdx button onclick not working

我已经创建了一个按钮,我想在悬停和单击时更改其外观。 我没有错误,但是没有用。 单击它或将其悬停时,它不会更改图像。 显示的唯一图像是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"));
   ...
   ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM