【问题标题】:libGDX: Create TextButton with TextureRegionlibGDX:使用 TextureRegion 创建 TextButton
【发布时间】:2017-06-10 18:18:04
【问题描述】:

我有一个加载所有 TextureRegions 的类。

public class AssetLoader{

    public static TextureAtlas atlas;
    public static TextureRegion startButtonUp;
    public static TextureRegion startButtonDown;
    ...


    public static void loadAssets(){
        atlas = new TextureAtlas("images.atlas");   
        startButtonUp = atlas.findRegion("start_button_up");
        startButtonDown = atlas.findRegion("start_button_down");
    }
}

现在我想在不同的类中创建一个 TextButton,它使用我的 AssetLoader 中的 TextureRegion。我的实际工作流程是:

    buttonsAtlas = new TextureAtlas("images.atlas");
    buttonSkin = new Skin();
    buttonSkin.addRegions(buttonsAtlas);

    font = new BitmapFont();

    stage = new Stage(new FitViewport(800, 400, camera), game.batch);

    Gdx.input.setInputProcessor(stage);

    textButtonStyle = new TextButton.TextButtonStyle();

    textButtonStyle.up = buttonSkin.getDrawable("start_button_up");
    textButtonStyle.down = buttonSkin.getDrawable("start_button_down");

    textButtonStyle.font = font;

    button = new TextButton("Start", textButtonStyle);
    button.setHeight(AssetLoader.startButtonDown.getRegionHeight());
    button.setWidth(AssetLoader.startButtonDown.getRegionWidth());

    button.setPosition(0,100);

    stage.addActor(button);
    button.addListener(new ClickListener() {
        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
            /// start game
            game.setScreen(new PlayScreen(game));
            dispose();
        }
    });

但在这种情况下,我并不真的需要我的 AssetsLoader(除了 RegionWidth 和 RegionHeight),而且对于不同类中的每个按钮,它似乎都有很多代码。有没有什么办法可以更有效地解决这个问题?

【问题讨论】:

  • 有什么问题?
  • 我的问题是,这种方法好用还是有其他有效的方法?

标签: android libgdx textures


【解决方案1】:

AssetLoader 类有一个 textureAtlas 和两个 TextureRegion 的引用,这两个 TextureRegion 在该 textureAtlas 中。

如果要依赖AssetLoader,请创建AssetLoader 的对象并使用AssetLoaderatlas 而不是buttonsAtlas(textureAtlas 的新实例)。

或者忘记AssetLoader

button = new TextButton("Start", textButtonStyle);
button.setHeight(buttonsAtlas.findRegion("start_button_up").getRegionHeight());
button.setWidth(buttonsAtlas.findRegion("start_button_up").getRegionWidth());

这是在一个类中创建所有资产并在整个游戏中使用的好方法,如果可能,您应该在游戏中使用AssetManager

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    相关资源
    最近更新 更多