getX()
和getY()
返回演员在本地坐标系中的位置。你可以将这个位置转换成舞台坐标。
演员具有方法localToStageCoordinates(Vector2 localCoords)
,可将指定点从演员坐标系转换为舞台坐标系。
为本地位置创建一个类级变量。
Vector2 localPos= new Vector2();
在该本地位置设置值
localPos.set(myActor.getX(),myActor.getY());
Vector2 stagedPos=group.localToStageCoordinates(localPos);
stagedPos.x
和stagedPos.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;
}
}
输出结果为:
演员初始位置为: 100.0 和 100.0
在组上执行 moveBy 操作前,演员位置为:100.0 和 100.0
应用 moveBy 操作后,演员位置为:100.0 和 100.0
相对于舞台坐标系的位置为:150.0 和 150.0
group.localToStageCoordinates(new Vector2(actor.getX(),actor.getY()));
不会返回正确的值,但是actor.localToStageCoordinates(new Vector2(0,0));
会返回,虽然我可能错了,但我已经尝试过几次,以防万一这不能帮助您,请尝试我的示例。 - centenondlocalToStageCoordinates
并传入演员的坐标,与在演员上传入(0,0)是一样的。请查看源代码。向上转换时,向量(0,0)将被转换为组坐标系中的演员坐标,并向上传播。 - EntangledLoops