如何让相机更新与播放器精灵相同的帧?

时间:2019-05-09 14:26:15

标签: java libgdx

在LibGDX中,我想让我的OrthographicCamera随身携带玩家精灵。当前,播放器精灵首先被更新,然后在下一帧中,相机更新为播放器精灵在前一帧中的位置。这是一个例子: https://i.imgur.com/wxJUizU.gifv

我曾尝试将gameMap.render(cam)移至方法的底部,但不仅不能解决我的问题,而且还会将地图纹理放置在playersprite上方,因此您将无法看到当地图在地图下方移动时,玩家会再次进行礼貌。

以下是渲染播放器精灵和摄像机的代码:

Library                SSHLibrary
Library                Telnet
Suite Setup            Open Connection And Log In
Suite Teardown         SSHLibrary.Close All Connections

*** Variables ***
${HOST}                OMITTED.70
${Other-Host}            OMITTED.1
${OTHER-USERNAME}        OMITTED
${OTHER-PASSWORD}        OMITTED
${USERNAME}            OMITTED
${PASSWORD}            OMITTED



*** Test Cases ***

Acessando OLT para testes
    SSHLibrary.Execute Command  Telnet.Open Connection  ${Other-Host}
    SSHLibrary.Execute Command  Telnet.Login  ${OTHER-USERNAME}     ${OTHER-PASSWORD}  password_prompt=password

Verificando a saída do terminal
    ${saida}=  SSHLibrary.Execute Command   Telnet.Execute Command  dir
    BuiltIn.Log to console  ${saida}

*** Keywords ***
Open Connection And Log In
   SSHLibrary.Open Connection     ${HOST}
   SSHLibrary.Login               ${USERNAME}        ${PASSWORD}

2 个答案:

答案 0 :(得分:1)

通常,您希望将逻辑分为两部分:

  • 更新

  • 渲染

因此,在每一帧之前,您都要执行任何相关的计算。 您的相机当前更新时间较晚,因为直到gameMap更新后它才执行逻辑。 例如:

// Perform logic and update the state
public void Update () {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    cam.viewportHeight = Gdx.graphics.getHeight() / 3;
    cam.viewportWidth = Gdx.graphics.getWidth() / 3;

    gameMap.getTiledMapRenderer().getBatch().begin();

    cam.position.x = player.getX() + (player.getWidth() / 2);
    cam.position.y = player.getY() + (player.getHeight() / 2);
}

// Draw map
public void Render () {
    gameMap.render(cam);

    player.draw(gameMap.getTiledMapRenderer().getBatch());
    gameMap.getTiledMapRenderer().getBatch().end();

    cam.update();
}

我不熟悉您使用的框架,因此无法确定它是否可以运行,但是在任何游戏开发情况下使用框架时,这就是您要处理的事情。< / p>

答案 1 :(得分:0)

像Bradley一样,您应该将逻辑分为两部分:更新和渲染

创建一个udpate()方法,在其中放置所有要更新的内容,并首先在render()方法中调用此方法:

public void update(){
    //Update camera position
    cam.position.x = player.getX() + (player.getWidth() / 2);
    cam.position.y = player.getY() + (player.getHeight() / 2);
    //apply all updates to the camera before this the changed position won't apply to the camera
    cam.update();
}

@Override
public void render() {
    //First call the update method we created
    update();

    //Clear the Screen
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    //Render
    gameMap.render(cam);

    gameMap.getTiledMapRenderer().getBatch().begin();
    player.draw(gameMap.getTiledMapRenderer().getBatch());
    gameMap.getTiledMapRenderer().getBatch().end();
}

您无需致电:

cam.viewportHeight = Gdx.graphics.getHeight() / 3;
cam.viewportWidth = Gdx.graphics.getWidth() / 3;

每帧。在create()方法中调用它就足够了