stage摄像机无法移动

llycmphe  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(383)

在我的游戏中,我将我的对象表示为一个演员,因此所有的游戏对象都会在一个舞台上。不知什么原因,当我试着移动舞台上的摄像机时,它不起作用,或者实际上它似乎不起作用。我在0,0的位置添加了一个游戏演员。当我移动相机的位置时,演员仍然停留在左下角,尽管当我记录相机的位置时,它表明相机已经移动。

public class Striker extends Actor {

    private Sprite img;

    private World worldRef;
    private Body body;

    //constructor
    public Striker(float size, float x, float y, World world) {
        img = new Sprite(new Texture(Gdx.files.internal("Striker.png")));
        //mains the aspect size ratio
        img.setSize((275f / 300f) * size, size);
        img.setPosition(x, y);

        worldRef = world;

        //set up the physics
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(x,y);
        body = world.createBody(bodyDef);

    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        img.draw(batch);
    }

    @Override
    public void act(float delta) {
        super.act(delta);
    }

    @Override
    public float getX() {
        return body.getPosition().x;
    }

    @Override
    public float getY() {
        return body.getPosition().y;
    }

    @Override
    public float getWidth() {
        return img.getWidth();
    }

    @Override
    public float getHeight() {
        return img.getHeight();
    }
}

两个日志的结果显示相机的位置已经移动,但看起来不像。

public class StrikerScreen implements Screen {

    public static float WIDTH = 1920;
    public static float HEIGHT = 1080;
    public static float PPM = 200;

    private Launcher launcherRef;

    private OrthographicCamera camera;
    private FitViewport viewport;

    private World world;
    private Box2DDebugRenderer debugRenderer;

    private Striker striker;

    private Stage gameStage;

    //constructor
    public StrikerScreen(Launcher launcher) {
        launcherRef = launcher;

        world = new World(new Vector2(0, -9.8f), true);
        debugRenderer = new Box2DDebugRenderer();

        gameStage = new Stage();
        camera = (OrthographicCamera) gameStage.getCamera();
        viewport = new FitViewport(WIDTH / PPM, HEIGHT / PPM, gameStage.getCamera());
        viewport.apply();

        gameStage.setViewport(viewport);
        striker = new Striker(160f / PPM, 0, 0, world);
        gameStage.addActor(striker);

        gameStage.getCamera().translate(viewport.getWorldWidth() / 2f, 500f, 0);
        viewport.apply();
        camera.update();

        Gdx.app.log("StrikerScreen.java", "Camera position: " + gameStage.getCamera().position.toString());
        Gdx.app.log("StrikerScreen.java", "Camera size: " + gameStage.getCamera().viewportWidth + ", " + gameStage.getCamera().viewportHeight);

    }

    @Override
    public void show() {

    }

    public void update(float delta) {
        world.step(1 / 30f, 6, 2);
        gameStage.act(delta);
    }

    @Override
    public void render(float delta) {
        update(delta);
        debugRenderer.render(world, camera.combined);
        gameStage.draw();

    }

    @Override
    public void resize(int width, int height) {
        viewport.update(width, height, true);
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {

    }
}
u0sqgete

u0sqgete1#

在您发布的代码中,移动相机的唯一次数是在删除线屏幕构造函数中,在该构造函数中显式转换相机,以及在 resize 方法,在其中调用 viewport.update(width, height, true); 经过 trueviewport.update 告诉它将摄影机移动到(0,0)位于视口左下角的位置。自 resize 当您将屏幕设置为此屏幕时,将自动调用,这是您将相机设置到的最新位置。

相关问题