简体   繁体   中英

2 Borders of Screen Flickers

Im trying to make a simple game in LibGdx, using Tiled MapEditor and I have a little problem with rendering, left and bottom borders flickers whenever i move a camera. Pic Related: 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;
        }

    }

}

You update the cam before your set it's position rather than after, and the projection matrix is never updated. Hopefully these two things fix your problem.

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

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


}

should be

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM