【问题标题】:Problems rendering map position with LibGDX使用 LibGDX 渲染地图位置时出现问题
【发布时间】:2016-05-28 04:45:27
【问题描述】:

我一直在关注 Youtube 上的 LibGDX 教程,但在将 TiledMap 渲染到我的屏幕时遇到了问题。目前,我可以使用我的 HUD java 类渲染一些标签/图像

public class HUD {
public Stage stage; //the background
private Viewport viewport; //so the HUD stays fixed and the world can move
private int score;
private int timer;
private int bactoCount;
private Texture foodTexture, antioBioBottle;

//these are widgets
Label antioBioTxt;
Image antiBioImg;
Label countDown;
Label countTxt;
Image foodImg;
Label foodTxt;
Label bactoCountLabel;
Label bactoTxt;
//these images need to be buttons...


public HUD (SpriteBatch sb) {
    foodTexture = new Texture("Bacto food.png");
    antioBioBottle = new Texture("Antibioticbottle.png");
    bactoCount = 0;
    timer = 0;
    viewport = new FitViewport(BactoBuds.V_WIDTH, BactoBuds.V_HEIGHT, new OrthographicCamera());

    stage = new Stage(viewport, sb);
    //stage = new Stage();
    //use a Table to organise widgets on the stage

    Table table = new Table();
    table.top();
    table.setFillParent(true); //the table is the size of our stage

    antioBioTxt = new Label("Antibiotic", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    antiBioImg = new Image(antioBioBottle);



    // %03d means its 3 digits long
    //bitmap font sets the font to bit style
    //string.format for changing from a string to a int

    countDown = new Label(String.format("%03d", timer), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    countTxt = new Label("Time to flood:", new Label.LabelStyle(new BitmapFont(), Color.WHITE));

    foodImg = new Image(foodTexture);
    foodTxt = new Label("Food", new Label.LabelStyle(new BitmapFont(), Color.WHITE));

    bactoCountLabel = new Label(String.format("%06d", bactoCount), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    bactoTxt = new Label("Bacteria:", new Label.LabelStyle(new BitmapFont(), Color.WHITE));

    //if multiple labels use expandX then they all share an equal portion of the screen

    table.add(antioBioTxt).expandX().padTop(10);
    table.add(foodTxt).expandX().padTop(10);
    table.add(bactoTxt).expandX().padTop(10);
    table.add(countTxt).expandX().padTop(10);
    table.row();
    table.add(antiBioImg).expandX();
    table.add(foodImg).expandX();
    table.add(bactoCountLabel).expandX().align(Align.center);
    table.add(countDown).expandX().align(Align.center);

    stage.addActor(table);


}

}

这些在屏幕上呈现得很好。但是,当我尝试渲染背景 TMX 地图图像时,它会渲染到错误的位置。这几天我一直在搞乱代码,试图改变地图位置的位置。起初我只能看到地图的一个小角落,现在我已经完成了整个渲染,但它只占屏幕的 1/4。现在我不知道如何进行。

public class playScreen implements Screen{
private BactoBuds game;
private OrthographicCamera gamecamera;
private Viewport gameView;
private HUD HUD;
private TmxMapLoader mapLoader; //loads map to screen
private TiledMap map; //reference to map
private OrthogonalTiledMapRenderer renderer;




public playScreen (BactoBuds game) {
    this.game = game;



    gamecamera = new OrthographicCamera();
    gameView = new FitViewport(BactoBuds.V_WIDTH, BactoBuds.V_HEIGHT, gamecamera);

    HUD = new HUD(game.batch);

    mapLoader = new TmxMapLoader(); //make a new map loader, set map to maploader, then pass it to the renderer
    map = mapLoader.load("grassy.tmx");
    renderer = new OrthogonalTiledMapRenderer(map);


    gamecamera.setToOrtho(false);
    gamecamera.position.set(gameView.getWorldWidth() / 2, gameView.getWorldHeight() / 2, 0); //this changes map position
    renderer.setView(gamecamera);

}


@Override
public void show() {

}



@Override
public void render(float 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();


}

请原谅狡猾的编码,我很新,我还在学习好的做法。我认为这与相机定位有关,但换掉这些值似乎并没有改变任何东西。

public class BactoBuds extends Game {
public static final int V_WIDTH = 800;
public static final int V_HEIGHT = 480;
public static final String Title = "BactoBuds";
public SpriteBatch batch; //public to let other screens have access to images
private Texture img;
private Game game;
private Screen screen;
private OrthographicCamera camera;

public BactoBuds () {
    game = this;

}



@Override
public void create () {
    batch = new SpriteBatch();
    img = new Texture("badlogic.jpg");
    // change this to menuScreen later
    setScreen(new playScreen(this));


}

public void dispose () {
    batch.dispose();
    img.dispose();
}

@Override
public void render () {

    super.render();
}

public void resume (){

}

public void pause () {

}

}

感谢您的帮助!

【问题讨论】:

  • 您的 TiledMap 有多大?如果你需要缩放。您可以在 OrthogonalTiledMapRenderer 构造函数中设置比例
  • 感谢您的回复。我最终将背景图像更改为纹理,但如果我将代码更改回来,我一定会在之前进行缩放。我记得尝试使用 scaledViewPort 或类似的方法无济于事。设置“float unitScale”时,通常写成 2 或 2f 之类的东西吗?还是我需要在使用前声明它? (对不起,菜鸟问题)

标签: java android libgdx


【解决方案1】:

您需要在 render 方法中调用 camera.update()。

【讨论】:

  • 感谢您的回复!我尝试将 gamecamera.update() 添加到渲染方法,然后添加到 playscreen java 类中的 playscreen 方法。然而,这并没有改变背景地图的位置。然后,我将 camera.update() 添加到我的主要 BactoBuds java 类的渲染方法中,但它只是使用“java.lang.NullPointerException”使应用程序崩溃
【解决方案2】:

您是否尝试过移动相机?如果只有 1/4 的地图可见,则您的相机可能以 0,0 为中心,而您的地图将其用作纹理的左下角原点。

试试 camera.position.set(BactoBuds.V_WIDTH / 2, BactoBuds.V_HEIGHT / 2, 0);

【讨论】:

    猜你喜欢
    • 2014-09-18
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2016-02-27
    • 2020-03-22
    相关资源
    最近更新 更多