I think it looks somehow kind of professional if the app shows a little loading-screen at start. Of course this only makes sence if you really have things to load there.
If we put all our image into one big texture, we’ll need that all the time. Such things we can preload at the beginning.
In Libgdx there is a class called AssetManager for this. We create an object of this in our Game-class so that we can access it from all screens.
As well I added a new Screen called LoadingScreen.
If we put all our image into one big texture, we’ll need that all the time. Such things we can preload at the beginning.
In Libgdx there is a class called AssetManager for this. We create an object of this in our Game-class so that we can access it from all screens.
As well I added a new Screen called LoadingScreen.
public class TestGame extends Game{
AssetManager assets;
@Override
public void create() {
setScreen(new LoadingScreen(this));
}
}
In the constructor of the LoadingScreen we tell the Assetmanger with
assets.load(“FILE”,[TYPE OF ASSET].class) which assets should be loaded.public LoadingScreen(TestGame pGame) {
game=pGame;
// which assets do we want to be loaded
game.assets=new AssetManager();
game.assets.load("ui/myskin.json", Skin.class);
game.assets.load("graphics/testPack.atlas",TextureAtlas.class);
game.assets.load("ui/test.fnt",BitmapFont.class);
}
The assets should be loaded asynchronly while our screen is displayed. Therefore we have to call the method assets.update() in the render()-method. This method as well tells us, when the assets have finished loading.
if(game.assets.update()){
// all the assets are loaded
game.setScreen(new SplashScreen(game));
}
If you want to access your textures, etc. in the other screens you can get them with assets.get():
atlas = game.assets.get("graphics/testPack.atlas", TextureAtlas.class);
Now preloading the assets works great, but we want in the (at the moment really short) loading time to show a progressbar. I used these two NinePatches to build one.




The assets for the progressbar have to be loaded the old way.
// load the assets for the loading screen :D
font=new BitmapFont();
batch=new SpriteBatch();
emptyT=new Texture(Gdx.files.internal("load/empty.png"));
fullT=new Texture(Gdx.files.internal("load/full.png"));
empty=new NinePatch(new TextureRegion(emptyT,24,24),8,8,8,8);
full=new NinePatch(new TextureRegion(fullT,24,24),8,8,8,8);
// [...] render():
batch.begin();
empty.draw(batch, 40, 225, 720, 30);
full.draw(batch, 40, 225, game.assets.getProgress()*720, 30);
font.drawMultiLine(batch,(int)(game.assets.getProgress()*100)+"% loaded",400,247,0, BitmapFont.HAlignment.CENTER);
batch.end();
And you get this cool loadingscreen:
No comments:
Post a Comment