libgdx 演员不会改变位置

libgdx actor won't change position

如果一个 actor 被它自己的 actor.act() 方法移动,它会完美地更新位置。但是,如果我使用 moveBy() 方法或 moveBy 动作将演员移动 group,其中包含演员,演员移动,但它的 getX() 和 getY() 值与运动前保持一致。当 actor 没有被它的 act() 方法移动,而是从外部方法移动时,有没有办法让 actor 更新它的位置?

代码段如下所示:

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

getX()getY() return 演员在本地坐标中的位置。您可以将此位置转换为舞台坐标。

Actor 有方法 localToStageCoordinates(Vector2 localCoords) 将 actor 坐标中的指定点转换为舞台坐标中的指定点

为本地位置创建一个 class 级别变量。

Vector2 localPos= new Vector2();

在那个 localPos 和

中设置值
localPos.set(myActor.getX(),myActor.getY());
Vector2 stagedPos=group.localToStageCoordinates(localPos); 

stagedPos.xstagedPos.y 是您的要求。

测试

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

输出为:

Actor 的初始位置:100.0 和 100.0
在 Group 上 moveBy 之前的 Actor 位置是:100.0 和 100.0
在组上应用 moveBy 后,Actor 位置为:100.0 和 100.0
用舞台线定位。是:150.0 和 150.0