2017-07-11 18 views
0

ゲームでは、タイルを使ってマップを作成したり編集したりしています。しかし、これらのマップは、私がこれまでに作ったサンプルではロードされません。LibGDXでタイル貼りをしたときの黒い画面

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 and 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の回答をご覧ください。

関連する問題