2屏幕边界闪烁

时间:2015-09-27 16:25:42

标签: java libgdx

我试图在LibGdx中制作一个简单的游戏,使用Tiled MapEditor,我有一点渲染问题,每当我移动相机时左右边框都会闪烁。 相关图片:https://gyazo.com/63b9e364cd4b2e8154c1bd177c9ee990

MyGdxGame.java

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
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;

public class MyGdxGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture img;
    AssetLoad load;
    Player player;
    TiledMapRenderer tiledMapRenderer;
    TiledMap tiledMap;
    float unitScale = 1 / 32f;

@Override
public void create () {
    load = new AssetLoad();
    load.manager.finishLoading();
    if(load.manager.update()){
        batch = new SpriteBatch();
        player = new Player();
        tiledMap = new TmxMapLoader().load("tilemaps/321.tmx");
        tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap,unitScale);

        batch.setProjectionMatrix(player.cam.combined);
    }
}

@Override
public void render () {
    update();
    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin();
    tiledMapRenderer.setView(player.cam);
    tiledMapRenderer.render();
    batch.end();
}

public void update(){
    player.playerMove();

    player.cam.update();
    player.cam.position.set(player.x,player.y,0);


}

public void dispose(){

}

Player.java

    package com.mygdx.game;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.Input.Keys;
    import com.badlogic.gdx.graphics.OrthographicCamera;

    public class Player {
        public OrthographicCamera cam = new OrthographicCamera(1024,720);
    int x = 10;
    int y = 12;

    public Player(){
        cam.setToOrtho(false,30,20);
    }

    public void playerMove()
    {

        if(Gdx.input.isKeyPressed(Keys.W))
        {
            y += 1;
        }
        if(Gdx.input.isKeyPressed(Keys.A))
        {
            x -= 1;
        }
        if(Gdx.input.isKeyPressed(Keys.S))
        {
            y -= 1;
        }
        if(Gdx.input.isKeyPressed(Keys.D))
        {
            x += 1;
        }

    }

}

1 个答案:

答案 0 :(得分:0)

在设置凸轮之前而不是之后更新凸轮,并且永远不会更新投影矩阵。希望这两件事能解决你的问题。

public void update(){
     player.playerMove();

     player.cam.update();
     player.cam.position.set(player.x,player.y,0);


}

应该是

public void update(){
    player.playerMove();

    player.cam.position.set(player.x,player.y,0);
    player.cam.update();
    batch.setProjectionMatrix(player.cam.combined);

}