java - LibGDX:Sprite.setBounds 无法与 volatile 坐标和 InputAdapter 一起正常工作

标签 java libgdx

尝试处理移动图像的点击时遇到问题。

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

我不明白原因在哪里。

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

我什至在线比较了运动中的图像和边界区域的坐标 - 它们应该是相同的。

构造函数中的图像和处理程序:

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

关于java - LibGDX:Sprite.setBounds 无法与 volatile 坐标和 InputAdapter 一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59190372/

相关文章:

java - Libgdx 井字游戏

java - 如何使用java通过REST api中的post请求将多个json文档发送到marklogic数据库中?

java - 获取字符串形式的 MimeMessage 内容

java - 如何实现异步队列?

java - Selenium 无法识别 IE 中新打开的浏览器上的元素

java - Sprite 不显示在 Libgdx 中

android - 任务 ':android:validateSigningDebug' : as_sys_sec_alg_ideaCBC 执行失败

java - Lucene5.3.1中StandardFilter到底做了什么?

java - 防止使用 ModelBuilder.createArrow 创建时箭头较长时为 "fatter"

java - 检测 Sprite 是否被触摸的简单方法?