如果演员被自己的<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.example.ns.appversion1.MapActivityFragment1"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="96dip"
android:orientation="vertical"
android:weightSum="2" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="left"
android:background="@drawable/border"
android:text="Refreshing 1"
android:textColor="#000"
android:textSize="12sp" />
</LinearLayout>
</FrameLayout>
</RelativeLayout>
方法移动,它会完美地更新位置。但是如果我通过<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="#0288D1" />
</shape>
移动演员,其中包含演员,使用actor.act()
方法或group
操作,则演员移动,但其getX()和getY()值在运动前保持不变。当演员不是被moveBy()
方法移动而是被外部方法移动时,演员有没有办法更新它的位置?
该代码段如下所示:
moveBy
答案 0 :(得分:4)
getX()
和getY()
返回actor在本地坐标中的位置。您可以将此位置转换为舞台的坐标。
具有方法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;
}
}
输出是:
演员的初始位置:100.0和100.0
演员在MoveBy on Group之前的位置是:100.0和100.0
moveBy应用于Group后,Actor Position为:100.0和100.0
台阶线的位置。是:150.0和150.0