LibGdx平铺地图渲染问题

时间:2016-02-22 21:07:32

标签: java libgdx tiled

我正在尝试学习一本书来学习LibGDX游戏引擎但我在渲染平铺地图时遇到了问题。我想我用书写了相同的代码,但我无法得到相同的结果。

这是一款带有角色和地图的简单游戏。当我渲染我的角色和背景时,没有问题。

它看起来像这样;

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

但在我添加我的tmx地图屏幕后,只显示游戏的某些部分而没有地图.. 我不知道如何解决这个问题..我真的很困惑。

enter link description here

那些是我的GameManager和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();

}

}

我希望你能帮忙,我在网站上搜索找到同样的问题,但我不能......

1 个答案:

答案 0 :(得分:0)

static TiledMap map;

让我们在GameConstants类中定义地图文件的路径:

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

要对映射进行排队以进行加载,请将这些代码行添加到loadAssets()方法中 在finishedLoading()方法的调用之前的GameManager:

// 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适用于此 目的。在GameManager类中声明其实例:

public static OrthogonalTiledMapRenderer renderer;
// map renderer