夹紧相机到级别java的边缘

时间:2017-03-19 16:38:16

标签: java

我正在用Java创建一个侧滚动游戏。我的相机工作正常,但我希望相机夹到水平边缘。我让右侧正常工作,但左侧(开始)侧呢?

代码:

Camera.java

public class Camera {
    private float x, y;

    public Camera(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }

    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    public void tick (GameObject player) {

        if (player.getX() + (Reference.WIDTH / 2 - 8) >    Martian_Run.getMartianRun().getLm().getWidthOfCurrentLevel()) return;

        float targetX = -player.getX() + Reference.WIDTH / 2 + 16;
        float targetY = -player.getY() + Reference.HEIGHT / 2 + 64;
        float x = getX();
        float y = getY();
        setX((int) (x + (targetX - x) * 0.1));
        setY((int) (y + (targetY - y) * 0.1));

    }
}


Martian_Run.getMartianRun().getLm().getWidthOfCurrentLevel()
// Gives the width of the current level
Reference.WIDTH
// Width of the window

1 个答案:

答案 0 :(得分:0)

将相机x-pos初始化为(Reference.WIDTH / 2 - 8),并在if (player.getX() + (Reference.WIDTH / 2 - 8) < 0 return;中的另一个if语句下方添加tick()

这样,窗口的左侧将永远不会出现在关卡开头的左侧。