LibGDX - 如何让我的菜单画面切换到游戏画面? [英] LibGDX - how do I make my menu screen switch to the Game screen?
问题描述
当我运行应用程序我的菜单屏幕显示,但是当我点击屏幕开始玩游戏的游戏开始玩,但在菜单画面依然是他们覆盖的游戏。我知道这是因为游戏有音乐播放。我还要添加一个启动画面,在未来,但我并不担心,自己目前。我在这个新的所以请最好解释的事情,你可以。下面是用来实现这一目标的3个班。
公共类SlingshotSteve扩展游戏{
公共SpriteBatch批;
公共BitmapFont字体;
公共无效创建(){
一批=新SpriteBatch();
//使用LibGDX默认的Arial字体。
字体=新BitmapFont();
this.setScreen(新菜单(本));
}
公共无效渲染(){
super.render(); //重要!
}
公共无效的Dispose(){
batch.dispose();
font.dispose();
}
}
下面是主菜单屏幕
公共类菜单实现屏幕{
最后SlingshotSteve游戏;
OrthographicCamera摄像头;
公共菜单(最终SlingshotSteve GAM){
游戏= GAM;
摄像头=新OrthographicCamera();
camera.setToOrtho(假的,800,480);
}
@覆盖
公共无效渲染(浮动三角洲){
Gdx.gl.glClearColor(0,0,0.2F,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.font.draw(game.batch,欢迎来到弹弓史蒂夫!,100,150);
game.font.draw(game.batch,点击任何地方开始!,100,100);
game.batch.end();
如果(Gdx.input.isTouched()){
game.setScreen((屏幕)新GameScreen(游戏));
配置();
}
}
下面是游戏的画面
公共类GameScreen实现屏幕{
最后SlingshotSteve游戏;
OrthographicCamera摄像头;
//创建我们的2D图像
SpriteBatch批;
TextureRegion backgroundTexture;
纹理质感;
GameScreen(最终SlingshotSteve GAM){
this.game = GAM;
摄像头=新OrthographicCamera(1280,720);
一批=新SpriteBatch();
纹理纹理=新的纹理(Gdx.files.internal(background.jpg));
backgroundTexture =新TextureRegion(纹理,0,0,500,500);
音乐mp3Sound = Gdx.audio.newMusic(Gdx.files.internal(rain.mp3));
mp3Sound.setLooping(真正的);
mp3Sound.play();
}
公共无效渲染(){
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(backgroundTexture,0,0);
batch.end();
}
@覆盖
公共无效调整大小(INT宽度,高度INT){
}
@覆盖
公共无效暂停(){
}
@覆盖
公共无效简历(){
}
@覆盖
公共无效的Dispose(){
batch.dispose();
texture.dispose();
}
不知道,但我觉得你的渲染()
的方法 GameScreen
不叫。你必须实现的方法渲染(浮动三角洲)
的使用增量时间参数。
替换
公共无效渲染(){
//你的code
}
与
公共无效渲染(浮动三角洲){
//你的code
}
When I run the application my menu screen shows, but when I click the screen to begin playing the game the game begins playing but the menu screen is still their overlaying the game. I know this because the game has music playing. I'm also going to add a splash screen in the future but I'm not concerned about that right now. I'm new at this so please explain things as best as you can. Below are the 3 classes used to make this happen.
public class SlingshotSteve extends Game {
public SpriteBatch batch;
public BitmapFont font;
public void create() {
batch = new SpriteBatch();
//Use LibGDX's default Arial font.
font = new BitmapFont();
this.setScreen(new Menu(this));
}
public void render() {
super.render(); //important!
}
public void dispose() {
batch.dispose();
font.dispose();
}
}
Here is the main menu screen
public class Menu implements Screen {
final SlingshotSteve game;
OrthographicCamera camera;
public Menu(final SlingshotSteve gam) {
game = gam;
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.font.draw(game.batch, "Welcome to Slingshot Steve!!! ", 100, 150);
game.font.draw(game.batch, "Tap anywhere to begin!", 100, 100);
game.batch.end();
if (Gdx.input.isTouched()) {
game.setScreen((Screen) new GameScreen(game));
dispose();
}
}
Here is the game screen
public class GameScreen implements Screen {
final SlingshotSteve game;
OrthographicCamera camera;
// Creates our 2D images
SpriteBatch batch;
TextureRegion backgroundTexture;
Texture texture;
GameScreen(final SlingshotSteve gam) {
this.game = gam;
camera = new OrthographicCamera(1280, 720);
batch = new SpriteBatch();
Texture texture = new Texture(Gdx.files.internal("background.jpg"));
backgroundTexture = new TextureRegion(texture, 0, 0, 500, 500);
Music mp3Sound = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
mp3Sound.setLooping(true);
mp3Sound.play();
}
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(backgroundTexture, 0, 0);
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
batch.dispose();
texture.dispose();
}
Not sure but I think your render()
method on GameScreen
not called. you must implement method render(float delta)
that use delta time for parameter.
replace
public void render() {
// your code
}
with
public void render(float delta) {
// your code
}
这篇关于LibGDX - 如何让我的菜单画面切换到游戏画面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!