界面显示一条进度条,随assetsmanager的加载进度更新,开始加载时会提示玩家开始读取游戏,加载完成进度条闪烁并提示玩家读取完成并需要点击屏幕以开始游戏。进度条图像是用pixmap在内存中生成,所以不需要图片资源。游戏提示用到了android 提示框Toast的调用,这方面你可以看前面的博文:调用平台原生UI显示信息。或者你可以直接删掉这部分的代码,换成自己想要的效果。
package gdx.uitls.screen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.viewport.StretchViewport;
public class SimpleLoadingScreen extends ScreenAdapter{
private Stage stage;
private float percent;
private AssetManager as;
private MyGame game;
private Texture barTexture;
Image bar;
public SimpleLoadingScreen(MyGame game,AssetManager as) {
this.game=game;
this.as=as;
}
@Override
public void show() {
stage = new Stage(new StretchViewport(800, 480));
Pixmap pm=new Pixmap(4, 4, Pixmap.Format.RGB888);
pm.setColor(Color.WHITE);
pm.fill();
barTexture=new Texture(pm);
pm.dispose();
game.pr.showQuickTip("游戏开始读取");//这里用android的Toast,此处代码需要你之前完成平台UI接口调用的实现,可以参考不久前的一篇博文
//创建进度条
bar=new Image(barTexture);
bar.setColor(Color.GRAY);
bar.setSize(10, 10);
bar.setPosition(150,235);
//创建进度条背景槽
Image barbg=new Image(barTexture);
barbg.setSize(500, 10);
barbg.setPosition(400, 240, Align.center);
stage.addActor(barbg);
stage.addActor(bar);
// 将资源加入assetsmanager的读取队列。
as.load("data/fish.png",Texture.class);
as.load("data/sea.jpg",Texture.class);
as.load("data/bgm.mp3",Music.class);
}
@Override
public void resize(int width, int height) {
// Set our screen to always be XXX x 480 in size
stage.getViewport().update(width, height);
}
private boolean finished;
@Override
public void render(float delta) {
// Clear the screen
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (as.update()) {
if(!finished){
game.pr.showQuickTip("点击屏幕开始游戏");
bar.addAction(Actions.forever(Actions.sequence(Actions.fadeOut(0.3f), Actions.fadeIn(0.3f))));
finished=true;
}
if (Gdx.input.isTouched()) {
game.setScreen(new MainMenuScreen(game));
game.pr.showQuickTip("游戏提示!屏幕手势右滑弹出帮助");
}
}
// Interpolate the percentage to make it more smooth
percent = Interpolation.linear.apply(percent, as.getProgress(), 0.1f);
// Update the bar
bar.setWidth(500*percent);
// Show the loading screen
stage.act();
stage.draw();
}
@Override
public void hide() {
// Dispose the loading assets as we no longer need them
stage.dispose();
barTexture.dispose();
}
}