我只是想让 libgdx 在我触摸屏幕的任何地方创建一张图片。
这就是我所拥有的,但没有做任何事情
SpriteBatch batch;
Texture img;
@Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
public class MyInputProcessor implements InputProcessor {
public boolean touchDown (int x, int y, int pointer, int button) {
batch.begin();
batch.draw(img,Gdx.input.getX(),Gdx.input.getY());
batch.end();
return true;
}
... (the rest of the input methods)
如果你不知道,我真的不知道我在做什么,我认为这与 batch.draw()
位于 touchDown 中有关
方法而不是渲染区域,但我也无法从研究中找出如何以不同的方式做到这一点
或者也许这也是错误的,重点是我这样做是为了学习,所以希望正确的答案能帮助我理解关于 java 的一些重要的事情
最佳答案
LibGDX 与基本上所有游戏引擎一样,每次都会重新渲染整个场景 render()
叫做。 render()
以游戏的帧速率重复调用(如果您没有复杂且未经优化的游戏,通常为 60fps)。你通常做的第一个与绘图相关的事情是在 render()
方法是清除屏幕,您已经使用Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
完成了该操作。 。然后,您可以使用自上一帧以来可能发生的任何变化来重新绘制整个场景。
您正在尝试在渲染方法之外使用批处理绘制某些内容。在这种情况下,您是在触地时执行此操作。但由于您仅在触地时才执行此操作,因此该对象将在下次调用 render()
时出现并消失。 ,因此它只会在屏幕上显示 1/60 秒。如果您想使用输入处理器执行此操作,则需要将某些变量设置为 true 以指示渲染方法应该绘制它,并将其他变量设置为指示在何处绘制它。然后在 render()
方法,如果变量为 true,则绘制内容。
其次,x
和y
输入处理器获取的数据不一定(并且通常不)与x
相对应。和y
在OpenGL中。这是因为 OpenGL 有自己的坐标系,其大小不一定与屏幕坐标系完全相同。屏幕左上角为(0,0),Y 轴向下,屏幕的宽度和高度与屏幕上的实际像素数相匹配。 OpenGL 的屏幕中心为 (0,0),Y 轴向上,无论实际屏幕像素多少,屏幕的宽度和高度均为 2。
但是OpenGL坐标系是用投影矩阵修改的。 LibGDX 相机类使这变得更简单。对于 2D 绘图,您需要一个正交相机。您可以使用相机设置 OpenGL 世界的宽度和大小,还可以定位相机。然后将相机的计算矩阵传递给 SpriteBatch,以便它在 OpenGL 空间中定位场景。
因此,要将输入坐标转换为场景坐标,您需要使用该相机来转换坐标。
最后,LibGDX 无法神奇地知道它应该将输入命令传递给您创建的任何旧输入处理器。您必须通过调用Gdx.input.setInputProcessor()
来告诉它应该使用哪个InputProcessor。 .
所以要修复你的类:
SpriteBatch batch;
Texture img;
boolean isTouchDown;
final Vector3 touchPosition = new Vector3();
OrthographicCamera camera;
@Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
camera = new OrthographicCamera();
Gdx.input.setInputProcessor(new MyInputProcessor()); // Tell LibGDX what to pass input to
}
@Override
void resize (int width, int height) {
// Set the camera's size in relation to screen or window size
// In a real game you would do something more sophisticated or
// use a Viewport class to manage the camera's size to make your
// game resolution-independent.
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update(); // re-calculate the camera's matrices
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined); // pass camera's matrices to batch
batch.begin();
if (isTouchDown) { // Only draw this while the screen is touched.
batch.draw(img, touchPosition.x, touchPosition.y);
}
batch.end();
}
public class MyInputProcessor implements InputProcessor {
public boolean touchDown (int x, int y, int pointer, int button) {
isTouchDown = true;
touchPosition.set(x, y, 0); // Put screen touch coordinates into vector
camera.unproject(touchPosition); // Convert the screen coordinates to world coordinates
return true;
}
public boolean touchUp (int screenX, int screenY, int pointer, int button){
isTouchDown = false;
return true;
}
//... (the rest of the input methods)
}
关于java - libgdx 在触摸位置绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41625012/