【问题标题】:LibGdx Background not RenderingLibGdx 背景不渲染
【发布时间】: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


    【解决方案1】:

    试试这个

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

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

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

    【讨论】:

    • 谢谢你,这样就搞定了!我不敢相信我真的做到了,哈哈!
    【解决方案2】:

    你的相机位置设置对了吗?

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

    【讨论】:

    • 我以前没用过相机,我应该用一个吗?
    • 你肯定需要在某些时候使用相机(会容易得多)。我猜你的问题是屏幕偏移,正如下面提到的用户。您可以在此处找到非常详细的正交相机(即您需要的相机)教程。如果您在实施/理解方面遇到任何问题,请随时提出任何问题,我会为您提供帮助。
    猜你喜欢
    • 2013-08-14
    • 1970-01-01
    • 2020-08-20
    • 2013-01-23
    • 1970-01-01
    • 2011-12-16
    • 2016-02-23
    • 2014-03-17
    • 1970-01-01
    相关资源
    最近更新 更多