问题
I am making a game in libgdx. I use AssetManager class to load assets. When I start my game its takes more time to load assets on android device and show black screen at that time.How can I resolve that problem.
回答1:
Well it will show a blank screen. In order to solve this problem you can render something while loading of assets is done and as the loading is done you switch to menu screen.
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.shooter.waste.of.a.game.Assets;
import com.shooter.waste.of.a.game.utils.Game;
import com.shooter.waste.of.a.game.utils.Screen;
public class LoadingScreen extends Screen{
public AssetManager manager;
SpriteBatch batcher;
Game game;
OrthographicCamera cam;
TextureAtlas load;
float stateTime;
AtlasSprite bg;
public LoadingScreen(Game game,SpriteBatch batcher) {
super(game);
this.batcher = batcher;
this.game = game;
manager = new AssetManager();
manager.load("data/shoot",TextureAtlas.class);
manager.load("data/b.jpg",Texture.class);
cam = new OrthographicCamera(480, 800);
cam.position.set(480 / 2, 800 / 2,0);
load = new TextureAtlas("data/load");
bg = new AtlasSprite(load.findRegion("loadingscrnbg"));
}
@Override
public void render(float deltaTime)
{
update(deltaTime);
GLCommon gl = Gdx.gl;
gl.glClearColor(0, 0, 1f, 0.1f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
cam.update();
batcher.setProjectionMatrix(cam.combined);
batcher.begin();
batcher.draw(bg, 0, 0);
batcher.enableBlending();
batcher.end();
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void update(float deltaTime)
{
if(manager.update()) \\ gives true when all assets are loaded
{
Assets.load(manager);
game.setScreen(new GameScreen(game, batcher));
}
}
@Override
public void backKeyPressed() {
// TODO Auto-generated method stub
}
}
One important thing is that it will still show a blank screen for those sprites which u want to render during the loading to try to render only few things and no sounds at all.
Hope this helps u out.
回答2:
all you can do is you can add a loadng screen in the starting of your game and can play some music that time.
in this way user will know that the game is loading .. load all your assests in this screen and show a progress barr...
来源:https://stackoverflow.com/questions/17488012/how-to-decrease-loading-time-of-all-assets-in-libgdx