2017-07-11 73 views
0

對於我使用Tiled製作和編輯地圖的遊戲。當我運行我的文件(該文件會自動將遊戲畫面到此文件)在LibGDX中使用Tiled時的黑色屏幕

package com.bluezamx.magillion.screens; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Input; 
import com.badlogic.gdx.Screen; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.maps.MapObject; 
import com.badlogic.gdx.maps.objects.RectangleMapObject; 
import com.badlogic.gdx.maps.tiled.TiledMap; 
import com.badlogic.gdx.maps.tiled.TmxMapLoader; 
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; 
import com.badlogic.gdx.math.Rectangle; 
import com.badlogic.gdx.math.Vector2; 
import com.badlogic.gdx.physics.box2d.Body; 
import com.badlogic.gdx.physics.box2d.BodyDef; 
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer; 
import com.badlogic.gdx.physics.box2d.FixtureDef; 
import com.badlogic.gdx.physics.box2d.PolygonShape; 
import com.badlogic.gdx.physics.box2d.World; 
import com.badlogic.gdx.scenes.scene2d.Stage; 
import com.badlogic.gdx.utils.viewport.FillViewport; 
import com.badlogic.gdx.utils.viewport.Viewport; 
import com.bluezamx.magillion.Magillion; 
import com.bluezamx.magillion.utils.Constants; 

public class WorldScreen implements Screen { 

    private Magillion game; 
    private OrthographicCamera gameCam; 
    private Viewport gamePort; 
    private Stage stage; 

    //Tiled variables 
    private TmxMapLoader mapLoader; 
    private TiledMap map; 
    private OrthogonalTiledMapRenderer maprenderer; 

    // Box2D variables 
    private World world; 
    private Box2DDebugRenderer debug; 

    public WorldScreen (Magillion game) { 
     this.game = game; 
     gameCam = new OrthographicCamera(); 

     mapLoader = new TmxMapLoader(); 
     map = mapLoader.load("TestWorld.tmx"); 
     maprenderer = new OrthogonalTiledMapRenderer(map, 1/Constants.PPM); 

     gamePort = new FillViewport(Constants.WIDTH/Constants.PPM, Constants.HEIGHT/Constants.PPM, gameCam); 
     gameCam.position.set((gamePort.getWorldWidth()/2), (gamePort.getWorldHeight()/2), 0); 

     world = new World(new Vector2(0,0), true); 
     debug = new Box2DDebugRenderer(); 
     debug.SHAPE_STATIC.set(1, 0, 0, 1); 

     for(MapObject object : map.getLayers().get(1).getObjects().getByType(RectangleMapObject.class)) { 
      Rectangle rect = ((RectangleMapObject) object).getRectangle(); 

      bdef.type = BodyDef.BodyType.StaticBody; 
      bdef.position.set(rect.getX() + rect.getWidth()/2, rect.getY() + rect.getHeight()/2); 

      body = world.createBody(bdef); 

      shape.setAsBox(rect.getWidth()/2, rect.getHeight()/2); 
      fdef.shape = shape; 
      body.createFixture(fdef); 
     } 
    } 

    @Override 
    public void show() {} 

    private void update(float dt) { 
     handleInput(dt); 

     world.step(1/60f, 6, 2); 
     player.update(dt); 

     gameCam.update(); 
     maprenderer.setView(gameCam); 
    } 

    @Override 
    public void render(float dt) { 
     update(dt); 

     Gdx.gl.glClearColor(0, 0, 0, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

     debug.render(world, gameCam.combined); 
     game.batch.setProjectionMatrix(gameCam.combined); 
     maprenderer.render(); 

     game.batch.begin(); 
     player.draw(game.batch); 
     game.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() { 
     map.dispose(); 
     maprenderer.dispose(); 
     world.dispose(); 
     debug.dispose(); 
    } 
} 

,我只得到一個黑色的屏幕:但是,這些映射不會加載到目前爲止,我所做的樣本。地圖本身不顯示。地圖是640 * 480大,放置在標準的android/assets文件夾中。

我試着在網上找到的其他代碼中運行我的地圖,它在那裏工作。但我無法弄清楚我的問題。

回答

0

看起來好像您的地圖實際上已渲染,但您的視口只是不在地圖位置。 嘗試使用:的

gamecam.setToOrtho(false, gamePort.getWorldWidth()/2, gamePort.getWorldHeight()/2); 

代替

gameCam.position.set((gamePort.getWorldWidth()/2), (gamePort.getWorldHeight()/2), 0); 
1

您需要更新設備屏幕寬度和高度的視口。

@Override 
public void resize(int width, int height) { 
    // use true here to center the camera 
    gamePort.update(width,height,false); 
} 

看一看this答案,最近我加了一個解決方案。