Libgdx - 演员的位置不正确

时间:2014-05-06 03:45:53

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

我把头发拉了出来。我现在已经花了几个小时。这就是我在做的事情。我有一个演员的形象。我可以将此图像移动到我想要的任何地方。这很好。我添加了一个Slider,所以我也可以调整这个图像的大小。这就是问题出现的地方。一旦我调整它,我仍然可以移动它,但它不再是我的手指所在。如果它缩小了尺寸,它就是NE。如果它增加,那就是SE。 (我的手指)。无论如何,我不知道是什么导致了这一点。我花了一个小时写出不同的方程式来调整每个尺寸,但没有任何工作。此外,世界的界限也变得不正确。我不应该被允许将演员/图像移到屏幕之外。我做了一个简单的检查。 (actor.getX()< = 0)。我觉得我应该注意,在调整大小之前,一切正常。无论如何,边界检查很简单,对吧?好吧,如果我让我的图像变小,它会在到达边缘之前停止。如果我把它做得更大,它可以稍微进入边缘。对于相反的一面,除了这次,如果它更大,它会在它到达之前停止,如果它更小,它可以进入一点。在这里,让我给你看一些代码......

我的滑块表:

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之间(aprrox。)。我将这些除以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的一半高度。

这里有一个关于定位如何关闭的说明,以及它从墙上过早停止: enter image description here

所以基本上就是这样。调整大小后,界限和位置似乎不再重要。我完全迷失了,并且头痛得厉害。如果有人知道为什么会这样,我做错了什么,或者我怎么能解决这个问题,我会永远(好吧,也许不是)感激不尽。 :)







我在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

1 个答案:

答案 0 :(得分:1)

只是通过你的代码breezin',看起来调整大小滑块调整图像大小而不是容器。当我使用Circles并在它们上面绘制图像时,我遇到了类似的情况; hitbox和图像不会对齐。可怕的图片,传入! Enlarging images and not the containers!

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

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

这是你的代码中让我感到奇怪的一点。

相关问题