LibGdx背景不渲染

时间:2014-11-20 16:05:32

标签: java libgdx screen rendering

我正在为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教程

2 个答案:

答案 0 :(得分:0)

试试这个

backgroundImage = new Texture(Gdx.files.internal("startScreen.jpg"));

编辑:spriteBatch.draw(...,position.x,position.y);

你使用位置的宽度和高度,你不在屏幕之外

答案 1 :(得分:0)

您是否正确设置了相机位置?

您是否更新了相机并将投影矩阵设置为SpriteBatch?