Erl*_* D. 9 java coordinates libgdx
我想创建一个浮动帮助泡泡来介绍我的游戏的基本功能.这个气泡应浮在我希望它解释的Actor之上,如下图所示.

为了实现这一点,我想要Actor的坐标,在这种情况下是左按钮,然后我可以将泡泡Actor添加到舞台上的其他所有内容.最后一部分很简单,但我正在努力检索按钮的实际坐标,就像它在表格中一样.
这两个按钮被添加到表中,如下所示:
t.add(btnLab).expandX().center();
t.add(btnSea).expandX().center();
Run Code Online (Sandbox Code Playgroud)
我尝试过最明显的方法:
Vector2 loc = new Vector2(a.getX(), a.getY());
System.out.println("Loc: " + loc);
a.localToStageCoordinates(loc);
System.out.println("Loc: " + loc);
Run Code Online (Sandbox Code Playgroud)
这给了我(按sysout的顺序):[0.0,0.0]和[40.0,130.0].最后一个位置实际上是表格的位置,它填充了屏幕的蓝色区域.因此,这个位置显然错过了Table放置Actor的功能,并且无法使用(因为我最终只得到了Table的位置).
(我也试过在这里使用t.localToStageCoordinates,这是表.结果相同.)
我尝试的另一个解决方案是递归搜索所有父母:
private static Vector2 getLoc(Actor a) {
return getLoc(new Vector2(a.getX(), a.getY()), a.getParent());
}
private static Vector2 getLoc(Vector2 loc, Actor g) {
System.out.println("Location: " + loc + ", Actor: " + g);
loc.x += g.getX();
loc.y += g.getY();
if(g.getParent() == null) return loc;
return getLoc(loc, g.getParent());
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这给了我同样的东西.sysouts提供以下内容:
Location: [0.0:0.0], Actor: Table 40.0,130.0 944.0x508.0
Location: [40.0:130.0], Actor: Group 0.0,0.0 0.0x0.0
Location: [40.0:130.0], Actor: Group 0.0,0.0 0.0x0.0
Run Code Online (Sandbox Code Playgroud)
所以我似乎无法获得表格中各组/参与者的实际位置.
我该怎么做?
Jyr*_*117 21
我想我明白你困惑的地方.获取演员的舞台坐标实际上非常简单(假设屏幕对齐,未旋转,未缩放的演员).
你需要做的就是:
public static Vector2 getStageLocation(Actor actor) {
return actor.localToStageCoordinates(new Vector2(0, 0));
}
Run Code Online (Sandbox Code Playgroud)
你做错了是试图获取actor相对于表的位置(通过使用actor.getX()和actor.getY()),然后将该位置转换为舞台.这将最终为您提供表的位置,因为它将添加actor的位置.你真正想知道你的演员里面的新Vector2(0,0)在哪里.这告诉你演员的左下角在哪里.如果你想要左上角你将使用新的Vector2(0,actor.getHeight()),并以类似的方式使用其他角落.
此外,如果您正在处理活动并想知道活动的舞台位置,您只需执行以下操作:
@Override public void touchUp(final InputEvent event, final float x, final float y, final int pointer, final int button) {
if (pointer == Buttons.LEFT) {
Gdx.app.log("Event", "x: " + event.getStageX() + " y: " + event.getStageY());
}
}
Run Code Online (Sandbox Code Playgroud)
有关更完整的示例,请查看以下代码:
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
public class ActorStage implements ApplicationListener {
private Stage stage;
private Skin skin;
@Override public void create() {
Gdx.app.log("CREATE", "App Opening");
this.stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
Gdx.input.setInputProcessor(this.stage);
this.skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
this.skin.hashCode();
final Table table = new Table();
table.setFillParent(true);
final Button btnLab = new TextButton("Lab", skin);
final Button btnSea = new TextButton("Sea", skin);
setupButton(table, btnLab);
setupButton(table, btnSea);
this.stage.addActor(table);
Gdx.gl20.glClearColor(0f, 0f, 0f, 1);
Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
}
private void setupButton(Table table, final Button button) {
table.add(button).expandX().center();
button.addListener(new ClickListener() {
@Override public void clicked(InputEvent event, float x, float y) {
Gdx.app.log("XY", "[" + x + ", " + y + "]");
Gdx.app.log("Event", "[" + event.getStageX() + ", " + event.getStageY() + "]");
Gdx.app.log("Actor", "[" + button.getX() + ", " + button.getY() + "]");
Vector2 loc = new Vector2(button.getX(), button.getY());
Vector2 stageLoc = button.localToStageCoordinates(loc);
Gdx.app.log("ActorStage", "[" + stageLoc.x + ", " + stageLoc.y + "]");
Vector2 zeroLoc = button.localToStageCoordinates(new Vector2());
Gdx.app.log("ZeroStage", "[" + zeroLoc.x + ", " + zeroLoc.y + "]");
}
});
}
@Override public void render() {
this.stage.act();
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl20.glEnable(GL20.GL_BLEND);
this.stage.draw();
}
@Override public void dispose() {
Gdx.app.log("DISPOSE", "App Closing");
}
@Override public void resize(final int width, final int height) {
Gdx.app.log("RESIZE", width + "x" + height);
Gdx.gl20.glViewport(0, 0, width, height);
this.stage.setViewport(width, height, false);
}
@Override public void pause() {}
@Override public void resume() {}
}
Run Code Online (Sandbox Code Playgroud)
单击btnLab一次,单击btnSea一次然后关闭窗口,输出:
CREATE: App Opening
RESIZE: 800x600
XY: [18.0, 13.0]
Event: [200.0, 296.0]
Actor: [182.0, 283.0]
ActorStage: [364.0, 566.0]
ZeroStage: [182.0, 283.0]
XY: [6.0, 23.0]
Event: [588.0, 306.0]
Actor: [582.0, 283.0]
ActorStage: [1164.0, 566.0]
ZeroStage: [582.0, 283.0]
DISPOSE: App Closing
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,您想要"ZeroStage"消息,因为它可以为您提供舞台上演员的位置.
| 归档时间: |
|
| 查看次数: |
10365 次 |
| 最近记录: |