【问题标题】:Libgdx - Actor's position is incorrectLibgdx - 演员的位置不正确
【发布时间】:2014-05-06 03:45:52
【问题描述】:

我正在拔头发。我已经花了几个小时在这上面。这就是我正在做的事情。我有一个演员的形象。我可以将此图像移动到任何我想要的位置。这很好用。我添加了一个滑块,因此我也可以调整此图像的大小。这就是问题所在。一旦我调整它的大小,我仍然可以移动它,但它不再是我的手指所在的位置。如果它的大小缩小到NE,它就是。如果它增加了,那就是 SE。 (我的手指)。无论如何,我不知道是什么原因造成的。我花了一个小时编写不同的方程式来调整每个尺寸,但没有任何效果。此外,世界边界也变得不正确。我不应该被允许将演员/图像移出屏幕。我做一个简单的检查。 (actor.getX()

我的滑块表:

private Table buildSlidersLayer() {
        Table layer = new Table();
        layer.right().top();
        Label label = new Label("A Button Size", skinLibgdx);
        layer.add(label);
        layer.row();
        sldSize = new Slider(0.0f, 1.0f, 0.01f, false, skinLibgdx);
        layer.add(sldSize);
        sldSize.addListener(new ChangeListener() {

            @Override
            public void changed(ChangeEvent event, Actor actor) {
                sldSizeVal = sldSize.getValue();
                divFacAButton = ((((1.0f - sldSizeVal) - 0.01f) * (2.0f - 0.7f)) / (0.99f - 0.01f)) + 0.7f;
                aButton.setSize(120 / divFacAButton, 120 / divFacAButton);
            }

        });
        // sldSize.setPosition(60, 60);
        return layer;
    }

我的 divFacAButton 介于 0.7 和 2.0(大约)之间。我将 120 除以这些以获得大小。

现在,这就是我移动图像的方式:

aButton = new Image(skinCydeonWorld, "a-button");
        layer.addActor(aButton);
        aButton.setSize(aButton.getWidth() / 1.5f,
                aButton.getHeight() / 1.5f);
        aButton.setOrigin(aButton.getWidth() / 2, aButton.getHeight() / 2);
        aButton.setPosition(GamePreferences.instance.aButtonX,
                GamePreferences.instance.aButtonY);
        aButton.setRotation(180f);

        aButton.addListener(new DragListener() {

            @Override
            public void touchDragged(InputEvent event, float x, float y,
                    int pointer) {
                float xAspect = stage.getWidth() / Gdx.graphics.getWidth();
                float yAspect = stage.getHeight() / Gdx.graphics.getHeight();
                aButton.setPosition(
                        (Gdx.input.getX() - aButton.getHeight() / 2f) * xAspect,
                        ((Gdx.graphics.getHeight() - Gdx.input.getY()) - aButton
                                .getHeight() / 2f) * yAspect);
                checkAndAdjustPos(aButton);
                super.touchDragged(event, x, y, pointer);
            }

        });

我在这里移动图像。我将图像位置设置为我单击的任何位置,减去 x 上的一半宽度,以及 y 上的一半高度。

这是一个关于定位是如何关闭的说明,以及它在墙上过早停止的情况:

基本上就是这样。调整大小后,边界和位置似乎变得不正常。我完全迷路了,头疼得厉害。如果有人知道为什么会发生这种情况,我做错了什么,或者我该如何解决这个问题,我将永远(好吧,也许不)感激。 :)






我在 BrainfoG313 的帮助下找到了解决方案! 这是我的新代码:

aButton.addListener(new DragListener() {

            @Override
            public void touchDragged(InputEvent event, float x, float y,
                    int pointer) {
                xAspect = Constants.VIEWPORT_GUI_WIDTH / Gdx.graphics.getWidth();
    yAspect = Constants.VIEWPORT_GUI_HEIGHT/ Gdx.graphics.getHeight();
                aButton.setPosition(
                        (Gdx.input.getX() - (aButton.getWidth() * divFacAButton / 2f))
                                * xAspect,
                        ((Gdx.graphics.getHeight() - Gdx.input.getY()) - (aButton
                                .getHeight() * (divFacAButton < 1.3f ? 0f : divFacAButton) / 2f))
                                * yAspect);
                checkAndAdjustPos(aButton);
                super.touchDragged(event, x, y, pointer);
            }

        });

检查演员是否离开屏幕:

{
        if (actor.getX() - (aButton.getWidth() / 2 / divFacAButton) + 80f / 2f < 0)
            actor.setPosition(
                    (aButton.getWidth() / 2 / divFacAButton) - 80f / 2f,
                    actor.getY());
        if (actor.getX() + actor.getWidth()
                / (minSize + maxSize - divFacAButton) > stage.getWidth() * xAspect)
            actor.setPosition(stage.getWidth() * xAspect - actor.getWidth()
                    / (maxSize + minSize - divFacAButton), actor.getY());
        if (actor.getY() - (aButton.getHeight() / 2 / divFacAButton) + 80f / 2f < 0)
            actor.setPosition(actor.getX(),
                    (aButton.getWidth() / 2 / divFacAButton) - 80f / 2f);
        if (actor.getY() + actor.getHeight()
                / (minSize + maxSize - divFacAButton)/* / divFactor */> maxHeight * yAspect)
            actor.setPosition(actor.getX(), maxHeight * yAspect - actor.getHeight()
                    / (minSize + maxSize - divFacAButton));
    }

为了检查它是否离开屏幕,我使用了两个不同的公式/方程式:

对于 x 轴:

xPosition - (1/2currentWidth / divFacAButton) + 1/2defaultWidth = leftEdge

对于 y 轴:

yPosition - (1/2currentHeight / divFacAButton) + 1/2defaultHeight = bottomEdge

还有我的第二个公式/方程式:

对于 x 轴

xPosition + currentWidth / (minSize + maxSize - divFacAButton) = rightEdge

对于 y 轴

yPosition + currentHeight / (minSize + maxSize - divFacAButton) = topEdge

它并不完美,但它非常接近并且工作正常! :D

【问题讨论】:

  • 不确定,但我记得拖动图像有一个偏移量,因此如果您用手指在平板电脑上触摸拖动它,您就可以看到它。但我不知道你能不能改变它。

标签: java math input drag-and-drop libgdx


【解决方案1】:

只需轻松浏览您的代码,调整大小滑块似乎正在调整图像大小而不是容器。当我使用 Circles 并在它们上绘制图像时,我遇到了类似的情况。 hitbox 和图像不会对齐。可怕的画面,来了!

float xAspect = stage.getWidth() / Gdx.graphics.getWidth();

float yAspect = stage.getHeight() / Gdx.graphics.getHeight();

这就是你代码中让我好奇的地方。

【讨论】:

  • 是的!就是这样!所以它周围的虚拟盒子没有改变导致问题的原因?那么它仍然应用原始大小的框吗?
  • 检查我的编辑!看看是否将 xAspect 和 yAspect 更改为 thebutton.getWidth 而不是 stage.getWidth,或者找出一种方法来调整图像和容器的大小。
  • 好的。我回家后会这样做,然后告诉你它是如何工作的。 x/yAspect 无论屏幕大小如何,都会根据屏幕分辨率正确转换该点。我也应该为按钮制作另一个 x/yAspect 吗?
  • 尝试将舞台 w/h 换成按钮,但没有奏效。 ://
  • 这就是我认为正在发生的事情。您的第一条评论是正确的; “盒子”(舞台)内的图像正在调整大小,但由于舞台本身没有改变,它正在从设定的舞台尺寸绘制图像,从而导致此问题。在我发布的图像中,图像使用右上角作为调整大小的参考点。如果您希望它正确调整大小,则需要同时将更改应用于舞台和图像。使用浮点 xAspect = stage.getWidth() / Gdx.graphics.getWidth();将返回相同的确切框大小,因为舞台永远不会改变。有意义吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多