2016-12-13 50 views
0

我一直在嘗試將平鋪地圖渲染到我的LibGDX項目中,但沒有在屏幕上顯示,我從這裏按照這個教程http://www.gamefromscratch.com/post/2014/04/16/LibGDX-Tutorial-11-Tiled-Maps-Part-1-Simple-Orthogonal-Maps.aspx無法在libgdx中渲染平鋪地圖

難道問題是我沒有設置相機的位置嗎?

這是我的代碼

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

我說這個瓷磚設置爲平鋪的地圖編輯器,並創建了一個瓷磚片,然後把它在我的項目中

+0

這段代碼對我來說很好,除非沒有'jet'演員和我自己的地圖。試試教程中的tilesheet。如果你有任何錯誤或輸出 - 請在這裏發帖 – exenza

+0

我確實使用了本教程中使用的tilesheet,並且只是一個黑屏的問題,你如何使用教程中的tilesheet,我可能會錯誤地使用它。感謝extenza回覆 – Mahbadran

+0

我只是將你的代碼複製粘貼到我的ide並更改了文件名。如何上鍵,下鍵,右鍵,左鍵,num_1,num_2他們改變什麼? – exenza

回答

0

它的工作,問題是該程序無法找到瓷磚集,所以我改變了路徑(從.tmx文件)到它實際上的位置和它的工作。