如何使用Libgdx相应地调整TextButton的大小

时间:2014-06-14 14:37:24

标签: libgdx scene2d

正如您在下面的代码中看到的,我使用设备目标的宽度和高度创建了camera。然后,我创建stage的大小camera viewport,并为其添加Window(一切正常,直到此处的任何设备)。

但是,向TextButton添加Windows时,不会根据舞台大小重新调整大小。

那么,如何在任何设备中重新调整TextButton的大小?

    @Override
    public void create() {
        .
        .
        .
        // Define camera our view port in meters because of Box2D 
        camera = new OrthographicCamera(20,
                20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics.getWidth()));

        // create stage with the size of the viewport
        stage = new Stage(new StretchViewport(camera.viewportWidth,camera.viewportHeight));


        // create restart button
        atlas = new TextureAtlas("data/ui/atlas.pack");
        skin = new Skin(Gdx.files.internal("data/ui/menu_skin.json"), atlas);
        TextButton restartButton = Util.createTextButton("Restart", skin, "red",
                new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                timer=timeMax;
            }
        });


        // create a window and add the restart to it
        skin.add("texture", new Texture("data/texture.png"));

        WindowStyle windowStyle = new WindowStyle(new BitmapFont(), Color.WHITE, skin.newDrawable("texture"));

        windowTryAgain = new Window("", windowStyle);
        windowTryAgain.setMovable(false);
        //windowTryAgain.padTop(20);

        //stage.addActor(window);
        windowTryAgain.setPosition(0, 0);
        windowTryAgain.setSize(2,2);

        windowTryAgain.row().fill().expandX();
        windowTryAgain.add(restartButton).fill(0f, 0f);
        windowTryAgain.center();
        windowTryAgain.setVisible(false);


        stage.addActor(windowTryAgain);
        .
        .
        .
}



public class Util {
    public static TextButton createTextButton(String text, Skin skin,
            String styleName, EventListener listener) {
        TextButton textButton = new TextButton(text, skin, styleName);
        textButton.pad(10);
        //Set height does not work in my case
        //textButton.setHeight(0.1f);
        textButton.addListener(listener);
        return textButton;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以制作辅助相机并将其附加到舞台上。要缩放表格,只需缩放辅助摄像机的视口大小

一个例子是:

//Heres where you set the scale of your buttons and your table
Camera secondaryCamera = new Camera(Gdx.graphics.getWidth() * scale,
                                    Gdx.graphics.getHeight() * scale); 
Stage stage = new Stage();
Table table = new Table();
ImageButton button = new ImageButton(drawableUp, drawableDown, drawableChecked);
table.setFillParent(true);

stage.getViewport().setCamera(secondaryCamera);

table.add(ImageButton);
stage.addActor(table);

当您将其用于触控时,这非常有用。您可以将表格用于整个用户界面,并根据自己的喜好进行缩放,与其他游戏项目无关;