将相机放在LIBGDX中

时间:2016-03-16 15:36:30

标签: camera libgdx screen

我正在制作游戏,我希望相机位于中心位置,即。玩家必须能够看到Y和Y'(例如),而不仅仅是Y或Y'。    我需要完全如下图所示。 expected output screenshot

在上图中,播放器位于屏幕中间。但是在我的游戏中,我得到的是下面附图,

actual output

如您所见,我可以根据需要定位相机,但是当我这样做时,红色条纹出现在精灵下方。有没有办法摆脱这个红酒吧?

这是我的屏幕代码,

   public Playscreen( )
   {
    ....
    this.gamecam = new OrthographicCamera();                                                                                  
    this.gamePort = new StretchViewport(MyJungleGame.V_WIDTH / MyJungleGame.PPM, MyJungleGame.V_HEIGHT / MyJungleGame.PPM, gamecam);
      renderer = new OrthogonalTiledMapRenderer(map, 1 / MyJungleGame.PPM); 
    gamecam.position.set(gamePort.getWorldWidth() / 2,                                                                                               
    ....
  }

我的更新方法,

 public void update(float dt) {      

        int newwidth = Gdx.graphics.getWidth() / 2;
        int newheight = Gdx.graphics.getHeight()/3;
        gamecam.position.x = this.gamehero.heroBody.getPosition().x + gamePort.getWorldWidth() / newwidth;// wana keep track of mario whn it moves in x axis and dont wana move d cam whn mario moves in y axis
        gamecam.position.y = this.gamehero.heroBody.getPosition().y + gamePort.getWorldHeight() /3.5f;        
            .......
  }

我只是玩数字来获得以下结果,但我不知道如何摆脱这些酒吧。请帮忙 。我是libgdx的初学者。提前致谢

1 个答案:

答案 0 :(得分:0)

红色条是存在的,因为你要做的第一件事是清除屏幕并绘制背景颜色,后者默认为红色。

Gdx.gl.glClearColor(1f, 0f, 0f, 1f); // <-- makes it red
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

因此,您要么将其更改为更理想的颜色,要么将相机向上移动,使底部与您的世界底部相匹配。

    gamecam.position.y = this.gamehero.heroBody.getPosition().y + gamePort.getWorldHeight() /3.5f;

你在这里是正确的轨道,但我不明白硬编码的3.5f。如果你愿意的话:

gamecam.position.y = this.gamehero.heroBody.getPosition().y + gamePort.getWorldHeight() / 2f;

然后你的角色应该位于屏幕的底部。在示例屏幕截图中,您发布的角色位于屏幕底部边缘上方2个区域,因此:

float cameraPositionY = this.gamehero.heroBody.getPosition().y + gamePort.getWorldHeight() / 2f;
cameraPositionY-= tileHeight * 2;
gamecam.position.y = cameraPositionY;

这个过程会推动相机向下,你需要用瓷砖填满它,否则你仍会看到那个背景。

PS:

到处都不需要this。当存在名为完全相同的重载参数时,您只需要使用它来访问变量。

private int score;
private int highScore;
public void setScore(int score)
{
    this.score = score;
    if (score > highScore)... // <- No need to use "this" for highScore.
}