Libgdx:如何在屏幕渲染后加载资源?

时间:2016-02-08 16:42:49

标签: java android libgdx

我已经在我的加载屏幕this example上实现了加载屏幕,这是Game的孩子。这就是我的资产init方法和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())上加载我的资源,因为它导致黑屏。问题是加载资产后我的屏幕加载因此这使得我的加载屏幕没有用。如何在渲染屏幕后加载此方法?

2 个答案:

答案 0 :(得分:0)

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

这是一个如何完成的一般结构。在进入load方法之前调用资产管理器上的所有render方法。然后重复调用update,直到加载完所有内容。然后在继续之前获得对资产的引用。

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

}

答案 1 :(得分:0)

在尝试了这么多推荐的解决方案后,由于this blog post

,我终于设法解决了这个问题

我使用了一个回调,它显示了一个带有图像的android framelayout,作为资产异步加载。