1
\$\begingroup\$

How can I set an actor to an absolute / fixed position on my TiledMap, such that it does not follow the camera when I pan around?enter image description here

enter image description here

Currently my code looks like:

camera = new OrthographicCamera();
rootGroup = new Group();

castleMap = new TmxMapLoader().load("castleMap.tmx");
isoMapRenderer = new IsometricTiledMapRenderer(castleMap, 1/32f);

...

final FitViewport fitViewport = new FitViewport(worldWidth, worldHeight);
stage = new Stage(fitViewport);
rootGroup.setSize(stage.getWidth(), stage.getHeight());
stage.addActor(rootGroup);

barracks = new Barracks(barracksIcon, self);
rootGroup.addActor(barracks);

From there, whether I try to position the barracks actor using

barracks.setPosition(x, y);

or using

Vector2 vector = new Vector2(x, y);
vector = stage.screenToStageCoordinates(vector);
vector = rootGroup.stageToLocalCoordinates(vector);
barracks.setPosition(vector.x, vector.y);

I get the same result.

I emphatically do not want to use Tables.

Thanks in advance!

\$\endgroup\$
0
\$\begingroup\$

This seems too silly to be correct, but I achieved the desired result with the following:

In the method that handles the camera moving...

stage.addListener(new DragListener() {
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            return true;
        }
        @Override
        public void touchDragged(InputEvent event, float screenX, float screenY, int pointer) {
            final float x = Gdx.input.getDeltaX() * .05f;
            final float y = Gdx.input.getDeltaY() * .05f;

            camera.translate(-x,y);
            camera.update();

            final float destinationX = rootGroup.getX() + x;
            final float destinationY = rootGroup.getY() - y;

            rootGroup.setPosition(destinationX, destinationY);
            stage.act();
            stage.draw();
        }
    });

Rather than "setting" the object (or in this case, it's parent Group()) to a fixed position on the map, I dynamically move the entire group relative to the camera in real-time.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.