Java LibGDX:Sprite.setBounds不';t使用volatile坐标和InputAdapter时不能正常工作

Java LibGDX:Sprite.setBounds不';t使用volatile坐标和InputAdapter时不能正常工作,java,libgdx,Java,Libgdx,在尝试处理单击运动图像时遇到问题 我使用InputAdapter.touchDown()来处理单击并为图像创建精灵。然后通过Sprite.setBounds()设置边界。此外,事实上,问题在于:如果setBounds()中的坐标不变,则单击操作将得到正确处理。但是,如果您更改它们(例如position.x++),对象将开始运动,但不会读取单击 我不明白原因在哪里 我试图在方法之外创建一个可变变量,但这也没有带来任何结果。 我试着用batch.draw(img)代替img.draw(batch)—

在尝试处理单击运动图像时遇到问题

我使用InputAdapter.touchDown()来处理单击并为图像创建精灵。然后通过Sprite.setBounds()设置边界。此外,事实上,问题在于:如果setBounds()中的坐标不变,则单击操作将得到正确处理。但是,如果您更改它们(例如position.x++),对象将开始运动,但不会读取单击

我不明白原因在哪里

我试图在方法之外创建一个可变变量,但这也没有带来任何结果。 我试着用batch.draw(img)代替img.draw(batch)——效果是一样的。 在img.setBounds()之后,我尝试将Gdx.input.setInputProcessor()重新定位到render()方法-没有任何更改

我甚至在线比较了Img和边界区域的坐标,在运动中——它们应该是一样的

构造函数中的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);
}

这样,我依次比较图像的XY坐标和鼠标点击点,得出结论:InputAdaper和SpRITE从上到下都是不同的。因此,X总是重合的,Y的值相差很大

因此,我为pic的中心输入了两个校正坐标xPos\yPos(从总场高中减去Y),并在touchDown()方法中,不与BoundRectangle进行比较,只比较pic坐标和点击模的差异。如果结果进入图像大小范围-一切正常

现在点击运动图像可以正常工作

   public void render(SpriteBatch batch, float radius, float boost) {
    speed+=boost; // rotational speed
    nextX = radius * (float) Math.cos(speed); // Offset step X
    nextY = radius * (float) Math.sin(speed); // Offset step Y

    // set image size and position
    img.setBounds(startPosition.x+nextX, startPosition.y+nextY, 100, 100);
    img.draw(batch);

    // Corrected coordinates of the image for InputAdapter coordinate system
    xPos = img.getX()+img.getWidth()/2;
    yPos = Gdx.graphics.getHeight()-img.getY()- img.getHeight()/2;

    // Check the coincidence of the coordinates of the image area and the click point
    Gdx.input.setInputProcessor(new InputAdapter(){
        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            if((Math.abs(xPos-screenX)<=img.getWidth()) && (Math.abs(yPos-screenY)<=img.getHeight()))
            {System.out.println("Image Clicked");}
            return true;
        }
    });
}
public void渲染(SpriteBatch批处理、浮动半径、浮动增强){
速度+=增压;//转速
nextX=radius*(float)Math.cos(speed);//偏移步长X
nextY=radius*(float)Math.sin(速度);//偏移步长Y
//设置图像大小和位置
img.setBounds(startPosition.x+nextX,startPosition.y+nextY,100100);
模拉(批);
//输入适配器坐标系的图像校正坐标
xPos=img.getX()+img.getWidth()/2;
yPos=Gdx.graphics.getHeight()-img.getY()-img.getHeight()/2;
//检查图像区域和单击点的坐标是否一致
setInputProcessor(新的InputAdapter(){
@凌驾
公共布尔接地(整数屏幕X、整数屏幕Y、整数指针、整数按钮){

如果((Math.abs(xPos screenX)是曾经调用过的
触地
方法,请设置一个断点并确保它是。然后将得到的边界矩形与屏幕坐标进行比较,以了解发生了什么wrong@Nicolas谢谢你的回答!在touchtown()方法中,点击效果很好(它可以在所有区域工作)​​屏幕,很明显),但“如果”块不起作用。我检查了边框的大小-没关系,100*100,就像图片一样。坐标-在屏幕内,X和Y与图片的坐标一致。