Java LibGDX精灵的移动速度是摄像机的两倍

时间:2017-08-21 17:10:22

标签: java android libgdx

所以我有一个extends ApplicationAdapter implements InputProcessor的课程,当你在屏幕上拖动时会做以下事情。

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {

    float x = Gdx.input.getDeltaX();
    float y = Gdx.input.getDeltaY();

    if (Store.isGameState) {
        Store.Entity.player.setxMove(x);
        Store.Entity.player.setyMove(-y);
    }
    return true;
}

在我的播放器类中,我有一个更新方法,它执行以下操作:

    @Override
    public void update() {
        x += xMove;
        y += yMove;
        Store.Camera.Camera.position.set(Store.Entity.player.getXPos() + Store.Entity.player.getWidth() / 2, Store.Entity.player.getYPos() + Store.Entity.player.getHeight() / 2, 0);
        Store.Camera.Camera.update();
    }

和一个渲染方法:

public void render(SpriteBatch SB) {
    SB.begin();
    Store.Entity.sprite.setPosition(Store.Entity.player.getXPos(), Store.Entity.player.getYPos());
    Store.Entity.sprite.draw(SB);
    SB.end();
}

哪个全功能,相机会像我的精灵一样四处移动。然而,我的精灵不会以与我的相机相同的速度移动,我不能为我的生活弄清楚为什么会这样。精灵的移动速度大约是相机移动速度的两倍,并且不是我理想的播放器中心。

编辑:

所以在我的GameState中我有以下内容:

package com.imjoshscott.dataria.states;

import com.imjoshscott.dataria.Game;
import com.imjoshscott.dataria.Store;

public class GameState extends State {

    public GameState(Game game) {
        super(game);
        Store.isGameState = true;
        Store.Entity.getPlayer(game, Store.Entity.getSprite());
        Store.Graphics.getSpriteBatch();
        Store.Graphics.getTiledMap();
        Store.Graphics.getTiledMapRenderer();
        Store.Camera.getCamera();
        Store.Camera.getHudCamera();
        Store.Camera.Camera.setToOrtho(false, Game.GAME_WIDTH, Game.GAME_HEIGHT);
        Store.Camera.HudCamera.setToOrtho(false, Game.GAME_WIDTH, Game.GAME_HEIGHT);
        Store.Camera.Camera.viewportHeight = Game.GAME_HEIGHT / 2.5f;
        Store.Camera.Camera.viewportWidth  = Game.GAME_WIDTH / 2.5f;
    }

    @Override
    public void update() {
        Store.Graphics.tiledMapRenderer.setView(Store.Camera.Camera);
        Store.Entity.player.update();
    }

    @Override
    public void render() {
        Store.Graphics.tiledMapRenderer.render();

        Store.Entity.player.render(Store.Graphics.SB);
    }

}

Store类中的Camera内容:

public static class Camera {
    public static OrthographicCamera Camera;
    public static OrthographicCamera HudCamera;

    public static OrthographicCamera getCamera() {
        if(Camera == null)
            Camera = new OrthographicCamera();
        return Camera;
    }
    public static OrthographicCamera getHudCamera() {
        if(HudCamera == null)
            HudCamera = new OrthographicCamera();
        return HudCamera;
    }
}

编辑:显示更新&渲染方法

public void update() {
    moveCreature();
    Store.Entity.sprite.setPosition(Store.Entity.player.getXPos(), Store.Entity.player.getYPos());
    Store.Camera.Camera.position.set(Store.Entity.player.getXPos(), Store.Entity.player.getYPos(), 0);
    Store.Camera.Camera.update();
    xMove = 0f;
    yMove = 0f;
}


public void render(SpriteBatch SB) {
    SB.begin();
    Store.Entity.sprite.draw(SB);
    SB.end();
}

1 个答案:

答案 0 :(得分:2)

已更新我之前的回答是不正确的,在看到并检查项目时,我看到了render()类的GameState方法(在查看了几个地方但没有骰子之后)如果再次发现这个非常奇怪的问题,我会指示这个。)

public void render() {
    Store.Graphics.tiledMapRenderer.render();

    Store.Entity.player.render(Store.Graphics.SB);
}

我们需要添加的一件事来修复非常奇怪的问题是添加以下代码

Store.Graphics.SB.setProjectionMatrix(Store.Camera.Camera.combined);

所以我们有

public void render() {
    Store.Graphics.SB.setProjectionMatrix(Store.Camera.Camera.combined);
    Store.Graphics.tiledMapRenderer.render();

    Store.Entity.player.render(Store.Graphics.SB);
}

这意味着我们需要将投影矩阵设置为当前活动SpriteBatch以正确渲染内容。在渲染瓷砖之前设置它也是安全的,因为它使用与播放器相同的相机。