1

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

这是我的代码;

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 个按钮已设置,并且有动作和侦听器,但现在它们什么都不做,当我单出一个按钮进行测试时,它可以工作,但可以从屏幕上的任何位置单击该按钮,所以我认为这是一个问题按钮区域,但我不知道如何设置。

我尝试过 .getheight()、.getWidth 等,但还是一样。尽管在我的纹理图集中,所有图像的高度和宽度都相同,因为它们都生成了一张图像,这可能是我的问题吗?

4

2 回答 2

0

For your first problem, which is that your buttons doesn't do anything, I think it's because the event is propagated to all of your buttons, probably in the order they have been added to the Stage. The problem is that your touchDown() method returns true. It means that the propagation should stop and then the touchDown() method of your others actors doesn't get called. You feel like it does nothing but in fact it does something, it's just that your touchDown() method is empty.

For you second problem, it's probably because your didn't set the size of your actors.

于 2013-05-22T20:45:19.390 回答
0

实际上它比这更简单。您需要设置按钮的边界。使用.setBounds(x,y,width,height)按钮的方法,它们还没有尺寸。点击区域应该在设置边界之后。

看一下 Table layout libgdx并使用表格。

于 2013-05-20T09:05:41.177 回答