2

正如您在下面的代码中看到的那样,我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;
    }
}
4

1 回答 1

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);

当您将其用于触摸控制时,这非常有用。您可以为整个 UI 使用一个表格,并根据您的喜好将其缩放到独立于您的其他游戏项目;

于 2014-09-01T06:12:08.247 回答