【发布时间】: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),而且对于不同类中的每个按钮,它似乎都有很多代码。有没有什么办法可以更有效地解决这个问题?
【问题讨论】:
-
有什么问题?
-
我的问题是,这种方法好用还是有其他有效的方法?