java - tmx映射仅将两个 block 渲染到屏幕中,其余全部为黑色

标签 java android youtube libgdx

The print from my game

我正在通过youtube(https://www.youtube.com/watch?v=P8jgD-V5jG8&t=43s)上的教程来学习开发类似于supermario的游戏,该教程的作者在其github中提供的代码现在仍在工作,但这是游戏的完整代码,并且我仍在视频课6中,因为在这里他的成绩与我的不同(从图像中可以看到)。

如果您中有更多经验丰富的开发人员可以告诉我仅显示2个tmx映射的块,我错了,我将不胜感激。我为我的英语表示歉意,感谢您的帮助。

游戏的主要类别:
public class MarioBros extends Game {

public SpriteBatch batch;
public static final int V_WIDTH = 400;
public static final int V_HEIGHT = 208;


@Override
public void create () {
    batch = new SpriteBatch();
    setScreen(new PlayScreen(this));
}

@Override
public void render () {
    super.render(); //Delega o metodo render para as SCREENS que estão ativas.
}

@Override
public void dispose () {
    batch.dispose();

}

}

PlayScreen(我认为问题出在这里!):
public class PlayScreen implements Screen {
private MarioBros game;
private OrthographicCamera gamecam;
private Viewport gamePort;
private HUD hud;

private TmxMapLoader maploader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;


public PlayScreen(MarioBros game) {
    this.game = game;

    gamecam = new OrthographicCamera();
    gamecam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);
    gamePort = new FitViewport(MarioBros.V_WIDTH, MarioBros.V_HEIGHT, gamecam);
    gamePort.apply();

    hud = new HUD(game.batch);

    // Loading the map we made in Tile
    maploader = new TmxMapLoader();
    map = maploader.load("level1.tmx");
    renderer = new OrthogonalTiledMapRenderer(map);


}

@Override
public void show() {

}

public void handleInput(float dt) {
    if (Gdx.input.isTouched())
        gamecam.position.x += 100 * dt;

}

public void update(float dt) {
    handleInput(dt);

    gamecam.update();

}

@Override
public void render(float delta) {
    update(delta);

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    renderer.render();
    game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
    hud.stage.draw();
}

@Override
public void resize(int width, int height) {
    gamePort.update(width, height);
}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {
    renderer.dispose();
}

}

Hud:
public class HUD implements Disposable{
public Stage stage;
private Viewport viewport; // Novo viewport
private Integer worldTimer;
private float timeCount;
private Integer score;

Label countdownLabel; // Label é equivalente ao widget, nessa biblioteca gdx
Label scoreLabel;
Label timeLabel;
Label levelLabel;
Label worldLabel;
Label marioLabel;

public HUD(SpriteBatch sb){
    worldTimer = 300;
    timeCount = 0;
    score = 0;

    viewport = new FitViewport(MarioBros.V_WIDTH, MarioBros.V_HEIGHT, new OrthographicCamera());
    stage = new Stage(viewport, sb); // Stage é um bloco que você insere coisas (inicialmente soltas dentro dele)

    Table table = new Table(); // Prender os itens em uma tabela
    table.top(); //  Coloca no topo do nosso stage
    table.setFillParent(true); //  largura do nosso stage

    countdownLabel = new Label(String.format("%03d", worldTimer), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    scoreLabel = new Label(String.format("%06d", score), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    timeLabel = new Label("TIME", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    levelLabel =new Label("1-1", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    worldLabel =new Label("WORLD", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    marioLabel =new Label("MARIO", new Label.LabelStyle(new BitmapFont(), Color.WHITE));

    //expandX vai expandir X ao maximo, por isso devemos por em todos para eles dividirem igualmente o eixo X.
    table.add(marioLabel).expandX().padTop(10);
    table.add(worldLabel).expandX().padTop(10);
    table.add(timeLabel).expandX().padTop(10);
    table.row();
    table.add(scoreLabel).expandX();
    table.add(levelLabel).expandX();
    table.add(countdownLabel).expandX();

    //inserir a table no estagio
    stage.addActor(table);

}

@Override
public void dispose() {
    stage.dispose();
}

}

感谢您抽出宝贵的时间来帮助我,我已经整整一个星期都在努力解决此问题= /(sad)

最佳答案

只需将您的类(class)与所有教程的this源代码进行比较即可。

关于java - tmx映射仅将两个 block 渲染到屏幕中,其余全部为黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46408304/

相关文章:

java - setText 函数不会改变 TextView

java - 为什么没有 volatile ?

java - Queue.Poll() 返回 null 但 Queue.size() >0 在 java 队列中

java - 如何更改运行时中的方法注释值?

php - 无法在php脚本中加载youtube源。

android - 基于WebView的应用,YouTube视频没有全屏按钮

android - YouTube API for Android的java.lang.ClassCastException

java - 扫描仪无此元素异常

java - 当我尝试写入或更新 SQLite 数据库时,我的联系人应用程序停止

java - 从 google docs 电子表格中读取数据