java - Libgdx: How can I load assets after screen has already rendered? -


i have implemented loading screen this example on loading screen child of game. how asset init method , screen class looks like. init method in assets loads classes contain atlasregion. have tested method makes screen load black screen loads lot of resources.

    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); } 

this loading screen class:

    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 manager load assets loading screen         game.manager.load("data/images-ui/loading/loading.pack", textureatlas.class);         // wait until finished loading         game.manager.finishloading();          // initialize stage place         stage = new stage();          // our textureatlas manager         textureatlas atlas = game.manager.get("data/images-ui/loading/loading.pack", textureatlas.class);          // grab regions atlas , create 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 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 need static bar, can         // loadingbar = new image(atlas.findregion("loading-bar1"));          // add actors stage         stage.addactor(screenbg);         stage.addactor(loadingbar);         stage.addactor(loadingbg);         stage.addactor(loadingbarhidden);         stage.addactor(loadingframe);         stage.addactor(logo);          // add loaded, instance:         assets.instance.init(game.manager);     }      @override     public void resize(int width, int height) {         // set our screen xxx x 480 in size         //width = 480 * width / height;         //height = 480;         stage.getviewport().update(width, height, false);          // make background fill screen         screenbg.setsize(width, height);          // place logo in middle of screen , 100 px         logo.setx((width - logo.getwidth()) / 2);         logo.sety((height - logo.getheight()) / 2 + 100);          // place loading frame in middle of screen         loadingframe.setx((stage.getwidth() - loadingframe.getwidth()) / 2);         loadingframe.sety((stage.getheight() - loadingframe.getheight()) / 2);          // place loading bar @ same spot frame, adjusted few px         loadingbar.setx(loadingframe.getx() + 15);         loadingbar.sety(loadingframe.gety() + 5);          // place image hide bar on top of bar, adjusted few px         loadingbarhidden.setx(loadingbar.getx() + 35);         loadingbarhidden.sety(loadingbar.gety() - 3);         // start position , how far move hidden loading bar         startx = loadingbarhidden.getx();         endx = 440;          // rest of hidden bar         loadingbg.setsize(450, 50);         loadingbg.setx(loadingbarhidden.getx() + 30);         loadingbg.sety(loadingbarhidden.gety() + 3);     }      @override     public void render(float delta) {         // clear screen         gdx.gl.glclear(gl20.gl_color_buffer_bit);          if (game.manager.update()) { // load some, return true if done loading             if (gdx.input.istouched()) { // if screen touched after game done loading, go main menu screen                 game.setscreen(new mainmenuscreen(game));             }         }          // interpolate percentage make more smooth         percent = interpolation.linear.apply(percent, game.manager.getprogress(), 0.1f);          // update positions (and size) match percentage         loadingbarhidden.setx(startx + endx * percent);         loadingbg.setx(loadingbarhidden.getx() + 30);         loadingbg.setwidth(450 - 450 * percent);         loadingbg.invalidate();          // show loading screen         stage.act();         stage.draw();     }      @override     public void pause () {}      @override     public void resume () {}      @override     public void hide() {         // dispose loading assets no longer need them         game.manager.unload("data/images-ui/loading/loading.pack");     } } 

i need load screen load assets on assets.instance.init(new assetmanager()) since causing black screen. problem screen loads after loading assets hence makes loading screen of no use. how can load method after screen has rendered?

remove finishloading call init method. should it. finishloading forces assets finish loading not want here. calling update on repeatedly, doing way load assets asynchronously.

here's general structure how done. call load methods on asset manager before ever gets render method. call update repeatedly until loaded. references assets before continuing.

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 on }  public void render(float delta){     gdx.gl.glclear(gl20.gl_color_buffer_bit);      //load 1 asset @ time per frame until loading complete     if (assetmanager == null || !assetmanager.update()){         //maybe draw load screen image that's not texture that's being managed          //by assetmanager. play animation. otherwise,          //you can leave screen blank.          return;     }      //will here when asset manager done. first time, still     //need references assets     if (!loadcomplete)         onassetsloaded();      //the rest of render method. below here happens after assets loaded , asset references have been aquired  } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -