使用 libgdx 缩放 scene2d 按钮

2023-12-29

I don't know if it is just me, but drawables http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/utils/Drawable.html confuse me. Are they intended to keep the size they are given, or is there a way to scale the image within them? I understand they can use ninepatch to fill certain areas by stretching an image, but I want to scale the image that it stretches. I am using a TextButton http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/TextButton.html for my menu buttons, but they are way too big, and I would love to know how to scale them. I am retrieving the skin from an atlas, which has ninepatch images in it. Here is the settings screen: The settings screen Here are the images in the pack I am using: ninepatch images packed Here is the initialization of the TextureAtlas, and Skin:

buttonAtlas = new TextureAtlas(Gdx.files.internal("buttons/buttons.pack"));
buttonSkin = new Skin(buttonAtlas); 

这是该菜单按钮的初始化。

TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.up = Assets.buttonSkin.getDrawable("bluebutton");
textButtonStyle.down = Assets.buttonSkin.getDrawable("redbutton");
textButtonStyle.font = font;

buttonMenu = new TextButton("Menu", textButtonStyle);
buttonMenu.pad(20.0f);
buttonMenu.addListener(new ClickListener() {
    @Override
    public void clicked(InputEvent event, float x, float y) {
        game.fade.startFadeOut();
        super.clicked(event, x, y);
    }
});

也许我可以改变我把它放在桌子上的方式,或者我把桌子放在舞台上的方式?

table.add(buttonMenu).width(Gdx.graphics.getWidth()/4);
stage.addActor(table);

作为最后的手段,我想我可以尝试创建自己的可以缩放的文本按钮演员,谢谢您的时间。


首先,如果您知道图像太大:最好的方法是将图像本身缩放到较小的尺寸,然后使用TexturePacker 生成一个包含较小图像的新TextureAtlas。

无论如何,如果您确实愿意,可以使用 TableLayout 缩放图像按钮,请参阅示例:

table.add(buttonMenu).width(100).height(100);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 libgdx 缩放 scene2d 按钮 的相关文章