getX()
とgetY()
戻り位置:
スニペットは次のようになります。この位置をステージの座標に変換できます。
ローカル位置のクラスレベルの変数を作成し、ステージの座標になるように俳優の座標で指定された点を変換する方法localToStageCoordinates(Vector2 localCoords)
を有する
俳優。
Vector2 localPos= new Vector2();
セットそのlocalPosの値と
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;
}
}
出力は次のとおりです。俳優の
初期位置:グループのMoveByメソッドの前に100.0と100.0
俳優のポジションである:100.0〜100.0
MoveByメソッドは、グループ、俳優のポジションに適用された後100.0および100.0
ステージコード付きの位置。次のようになります:150.0と150.0
私はかなり正しい値を返しますが、新しいVector2(actor.getX()、actor.getY()))私は間違っているかもしれませんが、これと数回の戦いがあります。私の例を試してみると助かります。 – centenond
@centenond私がテストしたところ、うまくいっています。答えは – Aryan
それを見て、私はいつも間違っていたと思う – centenond