使用LibGDX渲染地图位置时出现问题

时间:2016-05-28 04:45:28

标签: java android libgdx

我一直在关注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 () {

}

}

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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