简体   繁体   English

舞台相机不会动

[英]Stage camera won't move

In my game, I have my objects represented as a actors, thus all of the game objects would be on a Stage. 在我的游戏中,我将自己的对象表示为演员,因此所有游戏​​对象都将位于舞台上。 For some reason when I try to move the Stage's camera around, it won't work, or actually it doesn't seem to work. 由于某些原因,当我尝试移动舞台的摄像头时,它无法工作,或者实际上似乎不起作用。 I have added a game Actor to the location of 0,0. 我已将游戏演员添加到0,0的位置。 When I translate the camera's position around, the Actor still stays at the bottom left corner, despite when I log the camera's position, it shows that the camera has moved. 当我平移相机的位置时,尽管我记录了相机的位置,Actor仍停留在左下角,这表明相机已移动。

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();
    }
}

The results of the 2 logs show that the camera's positions have moved, but it doesn't look like it. 这两个日志的结果表明相机的位置已移动,但看起来并不像它。

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() {

    }
}

In the code you posted, the only times you move the camera are in the StrikerScreen constructor where you explicitly translate it, and in the resize method, where you have called viewport.update(width, height, true); 在发布的代码中,只有在StrikerScreen构造函数中显式地转换摄像机,以及在resize方法中才调用摄像机,仅在其中调用viewport.update(width, height, true); Passing true to viewport.update tells it to move the camera to where (0, 0) is in the bottom left of corner of the viewport. true传递给viewport.update告诉它将相机移动到viewport.update口角左下方的(0,0)。 Since resize is automatically called when you set your screen to this screen, that is the most recent position you have set the camera to. 由于在将屏幕设置为此屏幕时会自动调用resize ,因此这是将相机设置到的最新位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM