簡體   English   中英

Libgdx-演員的位置不正確

[英]Libgdx - Actor's position is incorrect

我正在拔頭發。 我現在已經花了幾個小時。 這是我在做什么。 我有一個演員形象。 我可以將此圖像移動到我想要的任何位置。 很好 我添加了一個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(大約)之間。 我將這些除以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

只需輕拂代碼即可,調整大小滑塊似乎在調整圖像大小,而不是在調整容器。 當我使用Circles並在其上繪制圖像時,我遇到了類似的情況。 匹配框和圖像無法對齊。 可怕的照片,即將到來! 放大圖像而不是容器!

浮動xAspect = stage.getWidth()/ Gdx.graphics.getWidth();

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

這就是您的代碼中令我感到奇怪的地方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM