Libgdx,如何从坐标创建矩形?

问题描述:

我目前正在尝试为RTS游戏制作一个选择器框.为此,我需要能够拖动鼠标以创建选择框,但这会导致长度/宽度为负.

I am currently trying to make a selector box for an RTS game. For this I need to be able to drag the mouse in order to create the selection box, however this can lead to a negative length/width.

在Libgdx中,有没有一种方法可以仅使用两组坐标来制作矩形?

In Libgdx is there a way to make rectangle from just using 2 sets of coordinates?

谢谢.

如果我了解您要执行的操作,这是一个简单的想法:

要创建一个矩形,可以使用它,Rectangle(float x, float y, float width, float height)有关更多信息,可以在这里阅读

to create a rectangle you can use this, Rectangle(float x, float y, float width, float height) for more inforamacion you can read it here http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Rectangle.html

这是一个或多或少的伪代码:

创建一个侦听器,以捕获击键,鼠标或相应的击键,

create a listener that captures keystrokes, mouse or them as appropriate,

捕获x,y并分配一个:

in touchdown catches x, y, and assign a:

yourVariableTouchDown.x = x;
yourVariableTouchDown.y = y;

然后当触摸屏捕获x时执行,并进行触摸,并分配一个点:

then when the touchup captures the x is executed, and the point where it makes up touch and assign a:

yourVariableTouchUp.x = x;
yourVariableTouchUp.y = y;

创建Rectagle之后:

after create the rectagle:

private Rectangle yourRectangle = new Rectangle();

yourRectangle(yourVariableTouchDown.x, yourVariableTouchDown.y,
              (yourVariableTouchDown.x - yourVariableTouchUp.x), 
              (yourVariableTouchDown.y - yourVariableTouchUp.y));

如果要查看它,可以使用ShapeRenderer: 看看这个 http://libgdx. badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/glutils/ShapeRenderer.html

if you want to see it you can use ShapeRenderer: look this http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/glutils/ShapeRenderer.html

添加变量类中的测试

private ShapeRenderer sRDebugRectangel = new ShapeRenderer();

添加更新或绘图中的测试

sRDebugRectangel.begin(ShapeType.Filled);
sRDebugRectangel.identity();

sRDebugRectangel.rect(yourRectangle.getX(), 
                      yourRectangle.getY(),
                      yourRectangle.getWidth(),
                      yourRectangle.getHeight());

sRDebugRectangel.end();

您可以查看该监听器的用法: https://www.google.es/#q=listener+libgdx

you can look on that listener use: https://www.google.es/#q=listener+libgdx

PS:您说的是否定的,将是要检查的是何时触摸小于创建矩形的触地变化,这正是我所遇到的,您已经对其进行了测试并调整了变量以创建矩形,因为您不能理想地在否定的情况下创建,现在我有时间来解决它,实际上我没有测试为什么我说这是伪代码,希望您提供服务,想法

P.S: what you say negative, will be a matter of check when touchup is less than touchdown change where the rectangle is created that was just what I happened to you have test it and adjust the variables to create the rectangle now because you can not be created desirably when negative, now I have time to get with it, in fact eh not tested this why I said it was pseudo code, hope you serve, idea

PS:您也可以查看此 https://stackoverflow.com/tour

P.S: You can also look at this https://stackoverflow.com/tour