【问题标题】:Libgdx: How can I load assets after screen has already rendered?Libgdx:屏幕已经渲染后如何加载资产?
【发布时间】:2016-02-08 16:42:49
【问题描述】:

我已经在我的加载屏幕上实现了来自 this example 的加载屏幕,它是 Game 的子级。这就是我的资产初始化方法和 Screen 类的样子。我在资产中的 init 方法加载包含我的 AtlasRegion 的类。我已经测试过这种方法是什么让我的屏幕加载了一个黑屏,因为它加载了很多资源。

    public void init (AssetManager assetManager) {
    this.assetManager = assetManager;
    // set asset manager error handler
    assetManager.setErrorListener(this);

    assetManager.load(Constants.TEXTURE_ATLAS_OBJECTS, TextureAtlas.class);
    assetManager.load(Constants.TEXTURE_ATLAS_UI, TextureAtlas.class);
    assetManager.finishLoading();

    TextureAtlas atlas = assetManager.get(Constants.TEXTURE_ATLAS_OBJECTS);
    TextureAtlas atlasUi = assetManager.get(Constants.TEXTURE_ATLAS_UI);

    //font = new BitmapFont(Gdx.files.internal("data/font.fnt"), Gdx.files.internal("data/font.png"), false);

    clickSound = Gdx.audio.newSound(Gdx.files.internal("data/sounds/click.wav"));

    // create game resource objects
    fonts = new AssetFonts(assetManager);
    skins = new AssetSkins();
    background = new AssetBackgroundImage();
    cards = new AssetCards(atlas);
    cardimages = new AssetImages(atlas);
    cardsjson = new AssetList();
    suitimages = new AssetSuitImages(atlas);
}

这是我的加载屏幕类:

    public class LoadingScreen extends AbstractGameScreen implements Disposable {

    private Stage stage;

    private Image logo;
    private Image loadingFrame;
    private Image loadingBarHidden;
    private Image screenBg;
    private Image loadingBg;

    private float startX, endX;
    private float percent;

    private Actor loadingBar;

    public LoadingScreen(CardGame game) {
        super(game);
    }

    public InputProcessor getInputProcessor () {
        return (null);
    }

    @Override
    public void show() {
        // Tell the manager to load assets for the loading screen
        game.manager.load("data/images-ui/loading/loading.pack", TextureAtlas.class);
        // Wait until they are finished loading
        game.manager.finishLoading();

        // Initialize the stage where we will place everything
        stage = new Stage();

        // Get our textureatlas from the manager
        TextureAtlas atlas = game.manager.get("data/images-ui/loading/loading.pack", TextureAtlas.class);

        // Grab the regions from the atlas and create some images
        logo = new Image(atlas.findRegion("libgdx-logo"));
        loadingFrame = new Image(atlas.findRegion("loading-frame"));
        loadingBarHidden = new Image(atlas.findRegion("loading-bar-hidden"));
        screenBg = new Image(atlas.findRegion("screen-bg"));
        loadingBg = new Image(atlas.findRegion("loading-frame-bg"));

        // Add the loading bar animation
        Animation anim = new Animation(0.05f, atlas.findRegions("loading-bar-anim") );
        anim.setPlayMode(Animation.PlayMode.LOOP_REVERSED);
        loadingBar = new LoadingBar(anim);

        // Or if you only need a static bar, you can do
        // loadingBar = new Image(atlas.findRegion("loading-bar1"));

        // Add all the actors to the stage
        stage.addActor(screenBg);
        stage.addActor(loadingBar);
        stage.addActor(loadingBg);
        stage.addActor(loadingBarHidden);
        stage.addActor(loadingFrame);
        stage.addActor(logo);

        // Add everything to be loaded, for instance:
        Assets.instance.init(game.manager);
    }

    @Override
    public void resize(int width, int height) {
        // Set our screen to always be XXX x 480 in size
        //width = 480 * width / height;
        //height = 480;
        stage.getViewport().update(width, height, false);

        // Make the background fill the screen
        screenBg.setSize(width, height);

        // Place the logo in the middle of the screen and 100 px up
        logo.setX((width - logo.getWidth()) / 2);
        logo.setY((height - logo.getHeight()) / 2 + 100);

        // Place the loading frame in the middle of the screen
        loadingFrame.setX((stage.getWidth() - loadingFrame.getWidth()) / 2);
        loadingFrame.setY((stage.getHeight() - loadingFrame.getHeight()) / 2);

        // Place the loading bar at the same spot as the frame, adjusted a few px
        loadingBar.setX(loadingFrame.getX() + 15);
        loadingBar.setY(loadingFrame.getY() + 5);

        // Place the image that will hide the bar on top of the bar, adjusted a few px
        loadingBarHidden.setX(loadingBar.getX() + 35);
        loadingBarHidden.setY(loadingBar.getY() - 3);
        // The start position and how far to move the hidden loading bar
        startX = loadingBarHidden.getX();
        endX = 440;

        // The rest of the hidden bar
        loadingBg.setSize(450, 50);
        loadingBg.setX(loadingBarHidden.getX() + 30);
        loadingBg.setY(loadingBarHidden.getY() + 3);
    }

    @Override
    public void render(float delta) {
        // Clear the screen
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        if (game.manager.update()) { // Load some, will return true if done loading
            if (Gdx.input.isTouched()) { // If the screen is touched after the game is done loading, go to the main menu screen
                game.setScreen(new MainMenuScreen(game));
            }
        }

        // Interpolate the percentage to make it more smooth
        percent = Interpolation.linear.apply(percent, game.manager.getProgress(), 0.1f);

        // Update positions (and size) to match the percentage
        loadingBarHidden.setX(startX + endX * percent);
        loadingBg.setX(loadingBarHidden.getX() + 30);
        loadingBg.setWidth(450 - 450 * percent);
        loadingBg.invalidate();

        // Show the loading screen
        stage.act();
        stage.draw();
    }

    @Override
    public void pause () {}

    @Override
    public void resume () {}

    @Override
    public void hide() {
        // Dispose the loading assets as we no longer need them
        game.manager.unload("data/images-ui/loading/loading.pack");
    }
}

我需要加载屏幕,然后在此 Assets.instance.init(new AssetManager()) 上加载我的资产,因为它是导致黑屏的原因。问题是加载资产后我的屏幕加载,因此这使我的加载屏幕毫无用处。屏幕渲染后如何加载此方法?

【问题讨论】:

  • 显示您的Assets.instance.init 方法。
  • 我已将其添加到问题中。
  • 我不完全确定我是否正确理解了您的代码和要求,但我认为您不应该在 init 方法中加载资产,而是为每个资产调用 game.manager.load。这样你可以先加载加载屏幕资源,然后调用 game.manager.finishLoading();最后在显示加载屏幕时加载其余资源。虽然可以说它是一个不那么优雅的解决方案,然后将所有内容加载到一个地方。
  • 感谢您的建议,但我确实需要使用从 game 加载 AssetManager 的 Assets 类。如果你的意思是加载每一个资产,我正在使用 game.manager 来做,这是我的 Assets 构造函数的参数。

标签: java android libgdx


【解决方案1】:

从您的 init 方法中删除 finishLoading 调用。那应该这样做。 finishLoading 强制资产立即完成加载,这不是您想要的。反复调用update,你已经在做的是异步加载资产的方法。

以下是如何完成此操作的一般结构。调用资产管理器上的所有load 方法,然后再调用render 方法。然后重复调用更新,直到所有内容都加载完毕。然后在继续之前获取对资产的引用。

private boolean loadComplete;

public void show(){
    //...
    loadComplete = false;
}

private void init(AssetManager assetManager){
    this.assetManager = assetManager;
    assetManager.load(/*whatever*/);
    assetManager.load(/*whatever*/);
    assetManager.load(/*whatever*/);
}

private void onAssetsLoaded(){
    loadComplete = true;
    myAtlas = assetManager.get("myAtlas.json", TextureAtlas.class);
    //and so on
}

public void render(float delta){
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    //load one asset at a time per frame until loading is complete
    if (assetManager == null || !assetManager.update()){
        //maybe draw a load screen image that's not a texture that's being managed 
        //by the assetManager. You could even play an animation. Otherwise, 
        //you can leave the screen blank.

        return;
    }

    //will only get here when asset manager is done. The first time, you still
    //need to get references to its assets
    if (!loadComplete)
        onAssetsLoaded();

    //the rest of you render method. Everything below here only happens after assets are loaded and asset references have been aquired

}

【讨论】:

  • 这样做会导致assetManager在加载之前尝试get() TextureAtlas。我应该如何在没有错误的情况下实现它?
  • @Zack 使用 isLoaded 方法。唯一的问题是您是否已经拥有加载屏幕的资源。我的理解是,您没有这样做,因为您将所有内容一起加载(加载屏幕和下一个屏幕的资源),在我的评论中我指的是这种情况,但也许我误解了这个问题。
  • @ChanandlerBong 'isLoaded() 检查是否加载了资产文件,但在这里我需要等待多个资产加载。你觉得我怎么能做到?
  • @Zack 我正在考虑为每个需要的资源调用 isLoaded() 并等待每个检查返回 true。如果您想等到所有内容都加载完毕,请检查 update() 是否返回 true 或 getProgress() 是否为 1。
  • @Zack 我添加了一个例子。
【解决方案2】:

在尝试了这么多推荐的解决方案后,我终于设法解决了这个问题,感谢this blog post

我使用了一个回调,它显示一个带有图像的 android 框架布局,因为资产异步加载。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多