(libgdx小结)切换游戏界面

可能这一节中奋斗的小土豆演示用的例子代码量有点多。其实切换界面的核心代码就是

使用形如"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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值