[英]How can i make my the user interface elements of my game adapt to the different mobile device screen in libgdx
[英]LibGDX - how do I make my menu screen switch to the Game screen?
当我运行该应用程序时,将显示菜单屏幕,但是当我单击屏幕以开始玩游戏时,游戏将开始播放,但菜单屏幕仍然覆盖了游戏。 我知道这是因为游戏中有音乐播放。 将来我还将添加一个初始屏幕,但现在我不担心。 我是新来的,所以请尽可能多地解释。 以下是用于实现此目标的3个类。
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();
}
}
这是主菜单屏幕
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();
}
}
这是游戏画面
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();
}
除了Phonbopit的答案。 您可能应该@Override GameScreen上的渲染功能。
不确定,但我认为GameScreen
上的render()
方法未调用。 您必须实现使用增量时间作为参数的方法render(float delta)
。
更换
public void render() { // your code }
与
public void render(float delta) {
// your code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.