libgdx - 相机没有正确跟随播放器 - 在屏幕上运行

时间:2014-10-09 01:55:50

标签: java libgdx topdown

我正在开发类似于口袋妖怪风格的libgdx rpg桌面游戏(自上而下的视图)。在游戏中,我使用OrthographicCamera跟随玩家走动使用Tiled创建的Tmx地图。我遇到的问题是,当玩家在地图中移动时,玩家最终会跑出相机,让他跑出屏幕而不是让他保持居中。我搜索过谷歌和YouTube,但没有找到解决问题的方法。

WorldRenderer.class

public class WorldRenderer {

private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
public OrthographicCamera camera;
private Player player;

TiledMapTileLayer layer;


public WorldRenderer() {
    map = new TmxMapLoader().load("maps/testMap2.tmx");
    renderer = new OrthogonalTiledMapRenderer(map);

    player = new Player();
    camera = new OrthographicCamera();
    camera.viewportWidth = Gdx.graphics.getWidth()/2;
    camera.viewportHeight = Gdx.graphics.getHeight()/2;
}
public void render (float delta) {
    camera.position.set(player.getX() + player.getWidth() / 2, player.getY() + player.getHeight() / 2, 0);
    camera.update();
    renderer.setView(camera);
    renderer.render();
    player.render(delta);
}
}   

Player.class

public class Player {

public Vector2 position; 
private float moveSpeed;
private SpriteBatch batch;

//animation
public Animation an;
private Texture tex;
private TextureRegion currentFrame;
private TextureRegion[][] frames;
private float frameTime;

public Player(){
    position = new Vector2(100, 100);
    moveSpeed = 2f;
    batch = new SpriteBatch();
    createPlayer();
}

public void createPlayer(){
    tex = new Texture("Sprites/image.png");
    frames = TextureRegion.split(tex, tex.getWidth()/3, tex.getHeight()/4);
    an = new Animation(0.10f, frames[0]);
}

public void render(float delta){
    handleInput();

    frameTime += delta;
    currentFrame = an.getKeyFrame(frameTime, true);

    batch.begin();
    batch.draw(currentFrame, position.x, position.y, 50, 50);
    batch.end();
}

public void handleInput(){
    if(Gdx.input.isKeyPressed(Keys.UP)){
        an = new Animation(0.10f, frames[3]);
        position.y += moveSpeed;
    }
    if(Gdx.input.isKeyPressed(Keys.DOWN)){
        an = new Animation(0.10f, frames[0]);
        position.y -= moveSpeed;
    }
    if(Gdx.input.isKeyPressed(Keys.LEFT)){
        an = new Animation(0.10f, frames[1]);
        position.x -= moveSpeed;
    }
    if(Gdx.input.isKeyPressed(Keys.RIGHT)){
        an = new Animation(0.10f, frames[2]);
        position.x += moveSpeed;    
    }
    if(!Gdx.input.isKeyPressed(Keys.ANY_KEY)){
        an = new Animation(0.10f, frames[0]);
    }
}

public void dispose(){
    batch.dispose();
}

public float getX(){
    return position.x;
}

public float getY(){
    return position.y;
}

public int getWidth(){
    return 32;
}

public int getHeight(){
    return 32;
}

}

1 个答案:

答案 0 :(得分:1)

问题如下:

player.render(delta);

使用SpriteBatch类中创建的PlayerSpriteBatch不使用Matrix的{​​{1}},因此它始终显示世界的同一部分,其中Camera用完屏幕。
现在有2个职位:
使用Player。为此,您需要在spriteBatch.SetProjectionMatrix(camera.combined)类中存储对camera的引用 或者使用更清洁的解决方案:分离逻辑和视图。使用一个类来绘制地图和播放器 因此,在Player而不是调用WorldRenderer,你应该player.render()那里的玩家。您可以使用draw s renderer进行绘图(SpriteBatch应该有效)。我想这renderer.getSpriteBatch()已使用SpriteBatch camera.combined,因此您无需关心此事。

希望它有所帮助。