4

If an actor is moved by its own actor.act() method, it updates the position flawlessly. But if I move the actor by a group, which contains the actor, with the moveBy() method or a moveBy action, the actor moves, but its getX() and getY() values stay the same as before the movement. Is there a way for the actor to update its position, when he is not being moved by its act() method, but rather from a outside method?

The snippet looks like this:

//Bounds are set in the MyActor class, which extends Actor
MyActor myActor= new MyActor();
Group group = new Group();
group.addActor(myActor)

//stage already created
stage.addActor(group);

//Doesn't update X and Y, when calling myActor.getX(), myActor.getY()
group.moveBy(x,y);
Stefan B
  • 541
  • 2
  • 6
  • 13

1 Answers1

4

getX() and getY() return position of actor in local coordinate. You can transform this position into stage's coordinate.

Actor having method localToStageCoordinates(Vector2 localCoords) that transforms the specified point in the actor's coordinates to be in the stage's coordinates

Create a class level Variable for local position.

Vector2 localPos= new Vector2();

set value in that localPos and

localPos.set(myActor.getX(),myActor.getY());
Vector2 stagedPos=group.localToStageCoordinates(localPos); 

stagedPos.x and stagedPos.y is your requirement.

Test

public class TestGame extends Game implements InputProcessor{

    Stage stage;
    Image image;
    Group group;
    Vector2 vector2=new Vector2();

    @Override
    public void create() {

        stage=new Stage();
        group=new Group();

        Gdx.input.setInputProcessor(this);

        image=new Image(new Texture("image/base.png"));
        image.setPosition(100,100);

        group.addActor(image);
        stage.addActor(group);

        System.out.println("Initial Position of Actor : "+image.getX()+" And "+image.getY());
    }

    @Override
    public void render() {
        super.render();

        Gdx.gl.glClearColor(1,1,1,1);
        gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.draw();
        stage.act();
    }

    @Override
    public void resize(int width, int height) {
        super.resize(width, height);
        stage.getViewport().update(width,height);
    }

    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {

        System.out.println("Actor Position Before moveBy on Group is : "+image.getX()+" And "+image.getY());

        group.moveBy(50,50);

        System.out.println("After moveBy applied on Group, Actor Position is : "+image.getX()+"And"+image.getY());
        vector2.set(image.getX(),image.getY());
        Vector2 stageCord=group.localToStageCoordinates(vector2);
        System.out.println("Position with Stage Cord. is : "+stageCord.x+" And "+stageCord.y);

        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
}

Output is :

Initial Position of Actor : 100.0 And 100.0
Actor Position Before moveBy on Group is : 100.0 And 100.0
After moveBy applied on Group, Actor Position is : 100.0 And 100.0
Position with Stage Cord. is : 150.0 And 150.0

Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
  • Im pretty sure that `group.localToStageCoordinates(new Vector2(actor.getX(),actor.getY()));` wont return the correct value, but `actor.localToStageCoordinates(new Vector2(0,0));` will, I might be wrong though but I have battle with this a few times, just in case this doesnt help you try my example – centenond Feb 12 '17 at 15:29
  • @centenond i tested and it's work fine as i explained, please check updated answer – Abhishek Aryan Feb 12 '17 at 17:08
  • look at that, guess I was doing it wrong all the time – centenond Feb 12 '17 at 17:15
  • @centenond Calling `localToStageCoordinates` on the group with the actor's coords is the same thing as calling on the actor with (0,0). Have a look at the source code. Vector (0,0) will be converted into the actor's coords in the group coordinate system and propagated upward. – EntangledLoops Jun 14 '20 at 06:04