LIBGDX使得在一个屏幕上5个不同的按钮 [英] LIBGDX making 5 different buttons on one screen

查看:128
本文介绍了LIBGDX使得在一个屏幕上5个不同的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5个按钮,我需要做不同的事情,但他们不这样做,我需要知道如何让他们做到这一点。

I have 5 buttons i need to make do different things but they don't and i need to know how to make them do it.

下面是我的code;

Here is my code;

public class MainMenu implements Screen {

CrazyZombies game;
Stage stage;
TextureAtlas atlas;
Skin skin;
SpriteBatch batch;
Button play, option, quit, custom, store, menu;

public MainMenu(CrazyZombies game) {
    this.game = game;
}

public void create () {
    stage = new Stage();
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0.09f, 0.28f, 0.2f, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

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

    batch.begin();
    batch.end();
}

@Override
public void resize(int width, int height) {
    if (stage == null)
        stage = new Stage(width, height, true);
    stage.clear();

    stage.setViewport(854, 480, true);
    stage.getCamera().translate(-stage.getGutterWidth(), -stage.getGutterHeight(), 0);

    Gdx.input.setInputProcessor(stage);

    /**
     * quit Button
     */

    TextButtonStyle styleQuit = new TextButtonStyle();
    styleQuit.up = skin.getDrawable("8layer");
    styleQuit.down = skin.getDrawable("8layer");

    quit = new Button(styleQuit);

    quit.addListener(new InputListener() {
        public boolean touchDown(InputEvent event, float x, float y,
                int pointer, int button) {
            return true;
        }

        public void touchUp(InputEvent event, float x, float y,
                int pointer, int button) {

        }
    });

    /**
     * End quit Button
     */

     /**
      * store Button
      */

    TextButtonStyle styleStore = new TextButtonStyle();
    styleStore.up = skin.getDrawable("9layer");
    styleStore.down = skin.getDrawable("9layer");

    store = new Button(styleStore);

    store.addListener(new InputListener() {
        public boolean touchDown(InputEvent event, float x, float y,
                int pointer, int button) {
            return true;
        }

        public void touchUp(InputEvent event, float x, float y,
                int pointer, int button) {
            game.setScreen(new StoreScreen(game));
        }
    });

    /**
     * End store Button
     */

     /**
      * customs Button
      */

    TextButtonStyle styleCustom = new TextButtonStyle();
    styleCustom.up = skin.getDrawable("10layer");
    styleCustom.down = skin.getDrawable("10layer");

    custom = new Button(styleCustom);

    custom.addListener(new InputListener() {
        public boolean touchDown(InputEvent event, float x, float y,
                int pointer, int button) {
            return true;
        }

        public void touchUp(InputEvent event, float x, float y,
                int pointer, int button) {
            game.setScreen(new CustomScreen(game));
        }
    });

    /**
     * End customs Button
     */

     /**
      * Options Button
      */

    TextButtonStyle styleOptions = new TextButtonStyle();
    styleOptions.up = skin.getDrawable("11layer");
    styleOptions.down = skin.getDrawable("11layer");

    option = new Button(styleOptions);

    option.addListener(new InputListener() {
        public boolean touchDown(InputEvent event, float x, float y,
                int pointer, int button) {
            return true;
        }

        public void touchUp(InputEvent event, float x, float y,
                int pointer, int button) {
            game.setScreen(new OptionScreen(game));
        }
    });

    /**
     * End Options Button
     */

     /**
      * Play Button
      */

    TextButtonStyle stylePlay = new TextButtonStyle();
    stylePlay.up = skin.getDrawable("7layer");
    stylePlay.down = skin.getDrawable("7layer");

    play = new Button(stylePlay);

    play.addListener(new InputListener() {
        public boolean touchDown(InputEvent event, float x, float y,
                int pointer, int button) {
            return true;
        }

        public void touchUp(InputEvent event, float x, float y,
                int pointer, int button) {
            Gdx.app.log(CrazyZombies.LOG, "un-touched");
            game.setScreen(new GameScreen(game));
        }
    });

    /**
     * End Play Button
     */

    /**
     * start Background
     */

    TextButtonStyle styleMenu = new TextButtonStyle();
    styleMenu.up = skin.getDrawable("background");

    menu = new Button(styleMenu);

    /**
     * End Background
     */

    stage.addActor(menu);
    stage.addActor(play);
    stage.addActor(option);
    stage.addActor(store);
    stage.addActor(custom);
    stage.addActor(quit);

}

@Override
public void show() {
    Audio.playMusic(true);
    batch = new SpriteBatch();
    atlas = new TextureAtlas("data/mainmenu/mainmenu.pack");
    skin = new Skin();
    skin.addRegions(atlas);
}

@Override
public void hide() {
    dispose();
}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void dispose() {
    batch.dispose();
    skin.dispose();
    atlas.dispose();
    stage.dispose();
    Audio.dispose();
}

public void playButton(Button play) {

}
}

所以,我的5个按键都设置有行动,和听众,但现在他们没有做任何事情,当我挑选一个按钮以测试它的工作原理,但该按钮可以从屏幕上的任何地方进行点击,所以我认为这是一个问题按钮区域,但我不知道如何设置它。

So my 5 buttons are set up and there actions and listeners but now they do not do anything and when i single one button out to test it works but the button can be clicked from any where on the screen so i think it is an issue with button areas but i do not know how to set it up.

我已tryed .getheight(),.getWidth等,但它仍然是相同的。虽然在我的纹理地图的高度和宽度的所有图像是一样的这一切让人图像可这是我的问题?

I have tryed .getheight(), .getWidth etc. but it is still the same. Although in my texture atlas the height and width for all the images is the same as it all makes one image could this be my issue ?

推荐答案

其实这是比这更简单。你需要设置你的按钮的边界。使用 .setBounds(X,Y,宽,高)的按钮的方法,他们没有一个规模呢。点击面积应设定界限之后。

Actually it's more simple than that. You need to set the bounds of your buttons. Use the .setBounds(x,y,width,height) method of the button, they do not have a size yet. The click area should be right after setting the bounds.

看看表布局libgdx ,并使用一个表。

Take a look at Table layout libgdx and use a table.

这篇关于LIBGDX使得在一个屏幕上5个不同的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆