2016-02-22 154 views
0

我試着去學習一本書來學習LibGDX遊戲引擎,但我有渲染瓷磚地圖的問題。我認爲我用書寫了相同的代碼,但我不能得到相同的結果。LibGdx瓷磚地圖呈現的問題

它是一個簡單的遊戲,帶有角色和地圖。當我渲染我的角色和背景時,沒有任何問題。

它看起來像這樣;

http://i66.tinypic.com/nb97nq.png

但之後,我加入我的TMX地圖屏幕顯示的遊戲,沒有地圖.. 我不知道如何解決這個..我真的困惑的只是一些部分。

enter link description here

這些都是我的遊戲管理器和ScreenManager類,也許你能找出我做錯了什麼..

public class GameManager { 

static TiledMap map; 
public static TiledMapRenderer renderer;///// 

//region paddle 
static TextureRegion leftPaddleTexture; 
static TextureRegion rightPaddleTexture; 
static Sprite leftPaddleSprite; 
static Sprite rightPaddleSprite; 
public static final float PADDLE_RESIZE_FACTOR = 700f; 
public static final float PADDLE_ALPHA = 0.25f; 
public static final float PADDLE_HORIZ_POS_FACTOR = 0.02f; 
public static final float PADDLE_VERT_POSITION_FACTOR = 0.01f; 
//endregion 

static AssetManager assetManager; 
static TextureAtlas texturePack; 

static Bob bob; 

static TextureRegion bobSpriteSheet; 

public static Sprite backgroundSprite; 
public static Texture backgroundTexture; 

public static final float BOB_RESIZE_FACTOR = 400f; 

public static void loadAssets() 
{ 
    assetManager.load(GameConstants.backgroundImage, Texture.class); 
    assetManager.load(GameConstants.texturePack, TextureAtlas.class); 


    assetManager.setLoader(TiledMap.class, new TmxMapLoader(new InternalFileHandleResolver())); 


    assetManager.load(GameConstants.level1, TiledMap.class); 


    assetManager.finishLoading(); 
} 

public static void initialize(float width, float height) 
{ 



    assetManager = new AssetManager(); 
    loadAssets(); 


    map = assetManager.get(GameConstants.level1); 
    renderer = new OrthogonalTiledMapRenderer(map, GameConstants.unitScale); 

    GameScreen.camera.setToOrtho(false, 35,20); 
    GameScreen.camera.update(); 


    renderer.setView(GameScreen.camera); 

    texturePack = assetManager.get(GameConstants.texturePack); 

    initializeLeftPaddle(width,height); 
    initializeRightPaddle(width, height); 

    bob = new Bob(); 
    bobSpriteSheet = texturePack.findRegion(GameConstants.bobSpriteSheet); 
    bob.initialize(width,height,bobSpriteSheet); 


    bob.bobSprite = new Sprite(bobSpriteSheet); 

    //set the size of the bob 
    bob.bobSprite.setSize((walkSheet.getRegionWidth()/ANIMATION_FRAME_SIZE) * (width/BOB_RESIZE_FACTOR), 
      walkSheet.getRegionHeight()*(width/BOB_RESIZE_FACTOR)); 

    bob.bobSprite.setPosition(width/2f, 0); 

    backgroundTexture =assetManager.get(GameConstants.backgroundImage); 
    backgroundSprite = new Sprite(backgroundTexture); 

    backgroundSprite.setSize(width, height); 

} 

public static void renderGame(SpriteBatch batch) 
{ 
    backgroundSprite.draw(batch); 
    bob.update(); 
    bob.render(batch); 
    leftPaddleSprite.draw(batch); 
    rightPaddleSprite.draw(batch); 

} 

public static void dispose() 
{ 
    assetManager.unload(GameConstants.backgroundImage); 
    assetManager.clear(); 
} 

public static void initializeLeftPaddle(float width, float height) 
{ 
    leftPaddleTexture = texturePack.findRegion(GameConstants.leftPaddleImage); 
    leftPaddleSprite = new Sprite(leftPaddleTexture); 

    leftPaddleSprite.setSize(leftPaddleSprite.getWidth()*width/PADDLE_RESIZE_FACTOR, 
      leftPaddleSprite.getHeight()*width/PADDLE_RESIZE_FACTOR); 

    leftPaddleSprite.setPosition(width * PADDLE_HORIZ_POS_FACTOR, height * PADDLE_VERT_POSITION_FACTOR); 

    leftPaddleSprite.setAlpha(PADDLE_ALPHA); 
} 

public static void initializeRightPaddle(float width, float height) 
{ 
    rightPaddleTexture = texturePack.findRegion(GameConstants.rightPaddleImage); 
    rightPaddleSprite = new Sprite(rightPaddleTexture); 

    rightPaddleSprite.setSize(rightPaddleSprite.getWidth()*width/PADDLE_RESIZE_FACTOR, 
      rightPaddleSprite.getHeight()*width/PADDLE_RESIZE_FACTOR); 

    rightPaddleSprite.setPosition(leftPaddleSprite.getX() + leftPaddleSprite.getWidth() + width * PADDLE_HORIZ_POS_FACTOR, 
      height * PADDLE_VERT_POSITION_FACTOR); 

    rightPaddleSprite.setAlpha(PADDLE_ALPHA); 
} 

}

public class GameScreen implements Screen { 

MainGame game; 
SpriteBatch batch; 
public static OrthographicCamera camera; 

public GameScreen(MainGame game) 
{ 
    this.game = game; 
    float height = Gdx.graphics.getHeight(); 
    float width = Gdx.graphics.getWidth(); 

    camera = new OrthographicCamera(width, height); 
    camera.setToOrtho(false); 

    batch = new SpriteBatch(); 

    GameManager.initialize(width, height); 

    Gdx.input.setInputProcessor(new InputManager(camera)); 

} 

@Override 
public void show() { 

} 

@Override 
public void render(float delta) { 
    Gdx.gl.glClearColor(1,1,1,1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 


    batch.setProjectionMatrix(camera.combined); 


    GameManager.renderer.render(); 


    batch.begin(); 
    GameManager.renderGame(batch); 
    batch.end(); 





} 

@Override 
public void resize(int width, int height) { 

} 

@Override 
public void pause() { 

} 

@Override 
public void resume() { 

} 

@Override 
public void hide() { 

} 

@Override 
public void dispose() { 
    batch.dispose(); 
    GameManager.dispose(); 

} 

}

希望你可以幫忙,我在網站上搜索找到相同的問題,但我couldnt ..

+2

如果你沒有得到有用的回答你的問題在這裏,請注意,堆棧交換網絡有一個專門的社區遊戲開發相關的問題,遊戲開發,其中你可能會找到更多的幫助。請參閱:http://gamedev.stackexchange.com/ – mech

+0

謝謝,我不知道。 –

+0

我看到的一件事與我所做的不同之處在於,我在渲染調用中設置了平鋪地圖渲染器的視圖,因此會發生每個渲染週期。還有另外一個樣品https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/superkoalio/SuperKoalio.java –

回答

0
static TiledMap map; 

讓我們定義在GameConstants類的映射文件的路徑:

public static final String level1 = "data/maps/level1.tmx"; 

要排隊地圖加載,之前添加幾行代碼到loadAssets()方法遊戲管理的 finishedLoading()方法的調用:

// set the tiled map loader for the assetmanager 
assetManager.setLoader(TiledMap.class, new TmxMapLoader(new 
InternalFileHandleResolver())); 
//load the tiled map 
assetManager.load(GameConstants.level1, TiledMap.class); 
//blocking method to load all assets 
assetManager.finishLoading(); 

現在,你可以得到的地圖實例加載到initialize()方法: loadAssets();

// get the map instance loaded 
map = assetManager.get(GameConstants.level1); 

要渲染地圖,我們需要一個地圖渲染器。由於我們使用的是正交映射,所以來自Tiled包的OrthogonalTiledMapRenderer適用於這個 的目的。聲明它的實例在遊戲管理類:

public static OrthogonalTiledMapRenderer renderer; 
// map renderer