【发布时间】:2014-11-20 16:05:32
【问题描述】:
我正在为我的 LibGdx Java 游戏使用多个屏幕,但在获取开始屏幕和渲染背景的死亡屏幕时遇到问题。正如我所检查的那样,背景位于正确的位置。 Main.java(游戏本身)可以工作,但没有别的。唯一能在启动和终止屏幕上工作的是键盘输入等输入。
代码: FishGame.java
package us.webco.fish;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class FishGame extends Game {
public SpriteBatch batch;
public void create() {
batch = new SpriteBatch();
this.setScreen(new StartScreen(this));
}
public void render() {
super.render();
}
public void dispose() {
batch.dispose();
}
}
StartScreen.java(空函数被删掉)
package us.webco.fish;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
public class StartScreen implements Screen{
private Texture backgroundImage;
private Sprite backgroundToSprite;
final FishGame game;
public StartScreen(final FishGame gam) {
game = gam;
backgroundImage = new Texture(Gdx.files.internal("../android/assets/startScreen.jpg"));
backgroundToSprite = new Sprite(backgroundImage);
backgroundToSprite.setSize(Main.width, Main.height);
}
@Override
public void render(float p) {
p = 1/60f; /* FPS */
Gdx.gl.glClearColor(0.4f,0.4f,0.7f,1.0f); /* Setting a default background color */
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); /* Rendering purposes */
game.batch.begin();
game.batch.draw(backgroundToSprite, Main.width, Main.height);
game.batch.end();
if(Gdx.input.isKeyPressed(Keys.X)) {
System.out.println("Start Game");
game.setScreen(new Main(game));
dispose();
}
if(Gdx.input.isKeyPressed(Keys.Z)) {
System.out.println("Game has been quit!");
Gdx.app.exit();
}
}
Main.java 也使用 game.batch。我不知道为什么它没有工作,因为我遵循了 LibGdx 教程,在此先感谢。
【问题讨论】:
标签: java libgdx screen rendering