【发布时间】:2020-03-30 02:28:25
【问题描述】:
在尝试处理点击移动图像时遇到问题。
我使用 InputAdapter.touchDown() 来处理点击并为图像创建 Sprite。然后我通过 Sprite.setBounds() 设置边界。此外,事实上,问题是:如果 setBounds() 中的坐标没有改变 - 点击被正确处理。但是,如果您更改它们(例如 position.x++) - 对象开始运动,但不会读取点击。
我不明白原因在哪里。
我试图在方法之外创建一个可变变量,但这也没有带来任何结果。 我尝试使用 batch.draw(img) 而不是 img.draw(batch) - 效果是一样的。 在 img.setBounds() 之后,我尝试将 Gdx.input.setInputProcessor() 重新定位到 render() 方法 - 没有任何改变。
我什至在线比较了运动中的 Img 和 Bounds 区域的坐标 - 它们应该是一样的。
构造函数中的Img和处理程序:
img = new Sprite(new Texture(finSize));
centerX = Gdx.graphics.getWidth()/2-img.getWidth()/2;
centerY = Gdx.graphics.getHeight()/2-img.getHeight()/2;
startPosition = new Vector2(centerX, centerY);
Gdx.input.setInputProcessor(new InputAdapter(){
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if(img.getBoundingRectangle().contains(screenX, screenY))
System.out.println("Image Clicked");
return true;
}
});
渲染:
public void render(SpriteBatch batch, float radius, float boost) {
speed+=boost;
nextX = radius * (float) Math.cos(speed); // Offset step X
nextY = radius * (float) Math.sin(speed); // Offset step Y
// Img is moving, but clicks is not handling
img.setBounds(startPosition.x+ nextX, startPosition.y + nextY, 100, 100);
// Handling clicks fine, but img is motionless
img.setBounds(startPosition.x, startPosition.y, 100, 100);
img.draw(batch);
// Checking coordinates - all's fine
System.out.println(img.getBoundingRectangle().getX());
System.out.println(startPosition.x + nextX);
System.out.println(img.getBoundingRectangle().getY());
System.out.println(startPosition.y + nextY);
}
【问题讨论】:
-
有没有调用过
touchDown方法?设置断点并确保它是。然后将您到达的边界矩形与屏幕坐标进行比较,以了解问题所在 -
@Nicolas 感谢您的回答!在 touchDown() 方法中,点击工作正常(显然,它在屏幕的所有区域都工作),但“if”块拒绝工作。我检查了 BoundingRectangle 的大小 - 没关系,100 * 100,就像图片一样。坐标——在屏幕内,X和Y与图片的坐标重合。