可能这一节中奋斗的小土豆演示用的例子代码量有点多。其实切换界面的核心代码就是
使用形如"game.setScreen(game.startScreen)"的代码即可
二、应用举例
单击屏幕实现界面切换
1、MyGame
package com.example.groupactiontest;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
public class MyGame extends Game {
StartScreen startScreen;
LoadingScreen loadingScreen;
Music music;
@Override
public void create() {
startScreen = new StartScreen(this);
loadingScreen = new LoadingScreen(this);
music = Gdx.audio.newMusic(Gdx.files.internal("BGMusic.ogg"));
music.play();
music.setLooping(true);
music.setVolume(15f);
this.setScreen(startScreen);
}
@Override
public void render() {
super.render();
}
}
2、StartScreen
package com.example.groupactiontest;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
public class StartScreen implements Screen {
Stage stage;
Texture texture;
SpriteBatch batch;
MyGame game;
public StartScreen(MyGame game) {
this.game = game;
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render(float arg0) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(texture, 0, 0);
batch.end();
stage.act();
stage.draw();
}
@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void show() {
stage = new Stage();
texture = new Texture(Gdx.files.internal("start.png"));
batch = new SpriteBatch();
stage.addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
game.setScreen(game.loadingScreen);
return true;
}
});
Gdx.input.setInputProcessor(stage);
}
}
3、LoaingScreen
package com.example.groupactiontest;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
public class LoadingScreen implements Screen {
MyGame game;
SpriteBatch batch;
Texture texture;
Stage stage;
public LoadingScreen(MyGame game) {
this.game = game;
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render(float arg0) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(texture,0,0);
batch.end();
stage.act();
stage.draw();
}
@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void show() {
stage = new Stage();
texture = new Texture(Gdx.files.internal("loading.png"));
batch = new SpriteBatch();
stage.addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
game.setScreen(game.startScreen);
return true;
}
});
Gdx.input.setInputProcessor(stage);
}
}
三、效果图
单击屏幕切换UI
四、源码下载
http://download.csdn.net/detail/caihongshijie6/7010473