2016-05-28 37 views
0

我一直在Youtube上跟隨LibGDX教程,並遇到了將TiledMap渲染到我的屏幕的問題。此刻,我可以用我的HUD Java類使用LibGDX渲染地圖位置的問題

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() { 

} 

}

感謝您的幫助!

+0

您的TiledMap有多大?如果你需要擴展。您可以在OrthogonalTiledMapRenderer構造函數中設置一個比例尺 –

+0

感謝您的回覆。我最終將背景圖像更改爲紋理,但是如果我將代碼更改回來,我一定會在縮放之前進行。我記得試圖使用scaledViewPort或一些這樣的無濟於事。 設置「float unitScale」時,通常是寫成類似2或2f的東西嗎?還是在我使用它之前需要聲明它? (對不起,對於noob問題) – Jediworm

回答

0

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

+0

感謝您的回覆!我嘗試將gamecamera.update()添加到render方法,然後添加到playscreen java類中的playscreen方法。但是這並沒有改變背景地圖的位置。然後,我在我的主要BactoBuds java類中將camera.update()添加到render方法,但它只是使用「java.lang.NullPointerException」崩潰應用程序 – Jediworm

0

您是否試過移動相機?如果只有地圖的1/4可見,則您的相機可能會以0,0爲中心,而您的地圖將該地圖用作紋理的左下角原點。

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