Search code examples
javalibgdxactorscene2d

libgdx actor won't change position


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

Solution

  • 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