【问题标题】:Can't render a Tiled Map in libgdx无法在 libgdx 中渲染平铺地图
【发布时间】:2016-12-13 08:59:02
【问题描述】:

我一直在尝试将平铺地图渲染到我的 LibGDX 项目,但屏幕上没有显示任何内容,我从这里 http://www.gamefromscratch.com/post/2014/04/16/LibGDX-Tutorial-11-Tiled-Maps-Part-1-Simple-Orthogonal-Maps.aspx 开始遵循本教程。

会不会是我没有设置好相机的位置?

这是我的代码

  package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.maps.tiled.AtlasTmxMapLoader;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage;

public class Level extends ApplicationAdapter implements InputProcessor {
    ActorDemo jet;
    Stage stage;
    TiledMap tiledMap;
    OrthographicCamera camera;
    TiledMapRenderer tiledMapRenderer;
    AtlasTmxMapLoader test;

    @Override
    public void create () {
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();
        jet = new ActorDemo();
        stage = new Stage();
        stage.addActor(jet);
        camera = new OrthographicCamera();
        camera.setToOrtho(false,w/2,h/2);
        camera.update();
        tiledMap = new TmxMapLoader().load("Level.tmx");
        tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
        Gdx.input.setInputProcessor(this);
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        camera.update();

        tiledMapRenderer.setView(camera.combined,0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
        tiledMapRenderer.render();
        stage.draw();
    }

    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        if(keycode == Input.Keys.LEFT)
            camera.translate(-32,0);
        if(Gdx.input.isKeyPressed(Input.Keys.A)){

            camera.position.x -=2;

        }
        if(keycode == Input.Keys.RIGHT)
            camera.translate(32,0);
        if(keycode == Input.Keys.UP)
            camera.translate(0,-32);
        if(keycode == Input.Keys.DOWN)
            camera.translate(0,32);
        if(keycode == Input.Keys.NUM_1)
            tiledMap.getLayers().get(0).setVisible(!tiledMap.getLayers().get(0).isVisible());
        if(keycode == Input.Keys.NUM_2)
            tiledMap.getLayers().get(1).setVisible(!tiledMap.getLayers().get(1).isVisible());
        return false;
    }

    @Override
    public boolean keyTyped(char character) {

        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
}

Screenshot of the Desktop configuration

Here is where I put my tiled map

This is the tile set I used to create my tmx file

我将此图块集添加到平铺地图编辑器并创建了一个图块表,然后将其放入我的项目中

【问题讨论】:

  • 这段代码对我来说很好用,除了没有jet 演员和我自己的地图。尝试教程中的 tilesheet。如果您有任何错误或输出 - 请在此处发布
  • 我确实使用了教程中使用的tilesheet,同样的问题只是黑屏,你是如何使用教程中的tilesheet的,我可能会用错了。感谢 extenza 回复
  • 我只是将您的代码复制粘贴到我的 ide 并更改了文件名。向上、向下、向右、向左、num_1、num_2 键怎么样?它们会改变什么吗?
  • 他们没有改变任何东西,只是黑屏,对任何输入都没有反应,代码对你有用吗?
  • 是的,它有效。能否提供完整的文件代码?

标签: java libgdx


【解决方案1】:

成功了,问题是程序找不到瓦片集,所以我将路径(从 .tmx 文件)更改为它实际所在的位置并且它工作了。

【讨论】:

    猜你喜欢
    • 2020-08-24
    • 2014-10-23
    • 2016-06-04
    • 2017-12-31
    • 1970-01-01
    • 2019-02-19
    • 2014-05-04
    • 2015-09-03
    • 2013-06-11
    相关资源
    最近更新 更多