libgdx点击目标上方的区域

时间:2016-10-01 14:03:38

标签: java android libgdx

我尝试制作一个带有可点击播放和退出标签的简单主菜单,但可点击区域始终位于标签上方。点击"播放"标签我需要点击它上方大约20个像素。

如何将可点击区域与标签对齐?

有我的MainMenuButton类

function disableSortingSearchOption(oTables, tableClass) {

    if (oTables != null) {
        var rowCount = oTables.fnSettings().fnRecordsDisplay();
        {

            if (rowCount == 0 || rowCount == 1) {


                var oSettings = oTables.fnSettings();

                //Remove sort icon
                $('.DataTables_sort_icon').remove();
                //Remove hand cursor
                $('.datatable th').css('cursor', 'default');
                //Iterate through each column and disable sorting 
                $('.' + tableClass + ' th').each(function (index) {
                    if ((oSettings.aoColumns[index]) != undefined) {
                        oSettings.aoColumns[index].bSortable = false;
                    }
                });
            }
        }
    }
}

我在MainMenu类中调用它

public class MainMenuButtons {
    private MainGame game;
    private Stage stage;
    private Viewport gameViewport;

    private Label mainGameLabel, playLabel, exitLabel;

    public MainMenuButtons(MainGame game) {
        this.game = game;

        gameViewport = new FillViewport(GameInfo.WIDTH, GameInfo.HEIGHT, new OrthographicCamera());
        stage = new Stage(gameViewport, game.getBatch());
        Gdx.input.setInputProcessor(stage);


        createLabelsAndButtons();
        addAllListeners();

        Table menuTable = new Table();
        menuTable.center().center();
        menuTable.setFillParent(true);
        menuTable.add(playLabel);
        menuTable.row();
        menuTable.add(exitLabel).padTop(40);


        Table gameTitle = new Table();
        gameTitle.center().top();
        gameTitle.setFillParent(true);
        gameTitle.add(mainGameLabel).padTop(40);

        menuTable.debug();
        stage.addActor(menuTable);
        stage.addActor(gameTitle);

    }

    void createLabelsAndButtons() {
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("Fonts/PressStart2P.ttf"));
        FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
        parameter.size = 45;

        BitmapFont font = generator.generateFont(parameter);
        mainGameLabel = new Label("Road Racer", new Label.LabelStyle(font, Color.WHITE));


        parameter.size = 25;

        font = generator.generateFont(parameter);
        playLabel = new Label("Play", new Label.LabelStyle(font, Color.WHITE));
        exitLabel = new Label("Exit", new Label.LabelStyle(font, Color.WHITE));
    }

    void addAllListeners() {

        playLabel.addListener(new ClickListener(){

            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                System.out.println("Play label clicked " + y);
//                game.setScreen(new GamePlay(game));
            }
        });

        exitLabel.addListener(new ClickListener(){

            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                System.out.println("Exit label clicked " + y);
//                Gdx.app.exit();
            }
        });



    }

    public Stage getStage() {
        return this.stage;
    }


}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我需要更新MainMenuButtons的视口,所以我创建了一个新方法

// in the MainMenuButtons class
public void viewportUpdate(int width, int height) {
    gameViewport.update(width, height);
}

并从MainMenu类调用if

@Override
public void resize(int width, int height) {
    gameViewport.update(width, height);
    btns.viewportUpdate(width, height);
}