如何在libgdx中保持演员进入舞台?

时间:2016-01-14 15:57:00

标签: java libgdx scene2d

我正在使用libgdx创建一个游戏,我正在为游戏中的实体使用舞台和演员。但问题是我不能让演员进入舞台或屏幕。当我在我的屏幕的最低,最高,最左或最右边拖动演员时,它会离开屏幕。我希望它留在屏幕内。

我该怎么办?请帮忙。

这是我用来拖动我的演员的代码条

addListener(new DragListener(){

       @Override
       public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
           camera.unproject(dimension.set(x, y, 0));
           System.out.println("clicked "+ x +","+y);
           return true;
       }

       @Override
       public void touchDragged(InputEvent event, float x, float y, int pointer) {
           moveBy(x-(getWidth()/2),y-(getHeight()/3));
       }

       @Override
       public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
           super.touchUp(event, x, y, pointer, button);
       }
   });

1 个答案:

答案 0 :(得分:1)

Stage确实有getWidthgetHeight方法。它们返回由内部Viewport管理的世界大小。 Window类具有可选的拖动行为和keepWithinStage参数。

这是LibGDX 1.8.0(Window类)的代码片段:

        public void touchDragged (InputEvent event, float x, float y, int pointer) {
            if (!dragging) return;
            float width = getWidth(), height = getHeight();
            float windowX = getX(), windowY = getY();

            float minWidth = getMinWidth(), maxWidth = getMaxWidth();
            float minHeight = getMinHeight(), maxHeight = getMaxHeight();
            Stage stage = getStage();
            boolean clampPosition = keepWithinStage && getParent() == stage.getRoot();

            if ((edge & MOVE) != 0) {
                float amountX = x - startX, amountY = y - startY;
                windowX += amountX;
                windowY += amountY;
            }
            if ((edge & Align.left) != 0) {
                float amountX = x - startX;
                if (width - amountX < minWidth) amountX = -(minWidth - width);
                if (clampPosition && windowX + amountX < 0) amountX = -windowX;
                width -= amountX;
                windowX += amountX;
            }
            if ((edge & Align.bottom) != 0) {
                float amountY = y - startY;
                if (height - amountY < minHeight) amountY = -(minHeight - height);
                if (clampPosition && windowY + amountY < 0) amountY = -windowY;
                height -= amountY;
                windowY += amountY;
            }
            if ((edge & Align.right) != 0) {
                float amountX = x - lastX;
                if (width + amountX < minWidth) amountX = minWidth - width;
                if (clampPosition && windowX + width + amountX > stage.getWidth()) amountX = stage.getWidth() - windowX - width;
                width += amountX;
            }
            if ((edge & Align.top) != 0) {
                float amountY = y - lastY;
                if (height + amountY < minHeight) amountY = minHeight - height;
                if (clampPosition && windowY + height + amountY > stage.getHeight())
                    amountY = stage.getHeight() - windowY - height;
                height += amountY;
            }
            lastX = x;
            lastY = y;
            setBounds(Math.round(windowX), Math.round(windowY), Math.round(width), Math.round(height));
        }

您可以通过将X钳位到0和stage.getWidth() - actor.getWidth()并对Y执行相同操作来简化此操作。