2

以下のコードでわかるようにcamera、デバイス ターゲットの幅と高さを使用して を作成します。次にstage、 のサイズで作成し、camera viewportそれに を追加Windowします (ここまでは、どのデバイスでもすべて正常に動作します)。

ただし、 に を追加するTextButtonWindows、ステージ サイズに応じてサイズが変更されません。

では、どのデバイスでも見栄えがするように 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