getX()
和getY()
返回位置。您可以將此位置轉換爲舞臺的座標。
有演員的方法localToStageCoordinates(Vector2 localCoords)
是變換指定的點在演員的座標是在舞臺的座標
創建本地位置的一流水平的變量。在localPos和
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));'會,我可能是錯誤的,但我有這個幾次戰鬥,以防萬一這不幫助你嘗試我的例子 – centenond
@centenond我測試,它的工作正常,因爲我解釋,請檢查更新回答 – Aryan
看看,猜測我一直在做錯事 – centenond