【问题标题】:LibGDX: Sprite.setBounds doesn't work correctly with volatile coordinates and InputAdapterLibGDX:Sprite.setBounds 不能与 volatile 坐标和 InputAdapter 一起正常工作
【发布时间】: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与图片的坐标重合。

标签: java libgdx


【解决方案1】:

所以,我依次比较了图像的 XY 坐标和鼠标点击点,得出的结论是 InputAdaper 和 Sprite 对 Y 的考虑不同——从上到下。所以X总是重合,Y的值相差很大。

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

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

   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;
        }
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 2014-07-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    相关资源
    最近更新 更多