简体   繁体   中英

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. 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.

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); 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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