使 Group 将 Actors 隐藏在其边界之外 [英] Making a Group hide Actors outside of its bounds

查看:8
本文介绍了使 Group 将 Actors 隐藏在其边界之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个非常简单的绘图,它通过在屏幕右侧添加新图像,然后使它们向左侧移动来动态更新.这样,屏幕边界内的绘图看起来就像是随着时间的推移而绘制的.

I have created a very simple plot that is dynamically updated by adding new images on the right side of the screen, and then making them move towards the left side. This way, the plot within the screen's bounds looks like they are plotted as time goes by.

不幸的是,这样做时,我必须让图像从屏幕外部开始,然后移动到视图中,并且在它们完全位于左侧屏幕之外之前,我无法再次删除它们.这会导致图像在屏幕边界之外部分可见.

Unfortunately, when doing it this way, I have to let the images start outside of the screen, and move into view, and I can't remove them again before they are completely outside of the screen on the left side. This results in the images being partially visible outside of the screen's bounds.

控件被创建为一个 Group,其中包含两个静态图像(黑色背景和作为前景的屏幕图像),以及介于两者之间的所有运动图像.这个 Group 然后被添加到我的 Stage 中(就像所有其他演员一样),并作为 Stage 的一部分进行绘制.

The control is created as a Group that contains two static images (a black background and the image of the screen as foreground), plus all the moving images in between. This Group is then added to my Stage (like all other actors), and drawn as part of the Stage.

在下图中,控件的边界用红色标记.我想隐藏超出这些范围的运动图像部分.使用我错过的一些 libGDX 功能可以做到这一点吗?也许是通过某种方式限制了 Group 的绘图区域?

In the image below, the control's bounds are marked with red. I would like to hide the parts of the moving images that are outside of these bounds. Is this possible to do using some libGDX functionality I have missed? By somehow constraining the Group's drawing area, perhaps?

我的其他选择是在屏幕前面绘制蓝色背景的一部分,隐藏异常值,或者使屏幕边缘足够厚以隐藏它们.我认为这两种解决方案都不必要地带来不便,而且我很确定有更好的方法来做到这一点.

My other alternatives are to draw parts of the blue background in front of the screen, to hide the outliers, or to make the screen's edges thick enough to hide them. I think that both these solutions are unnecessarily inconvenient, and I'm pretty sure there is a better way to do this.

有什么建议吗?

推荐答案

解决方案是使用 ScissorStack.

@Override
public void draw(SpriteBatch batch, float parentAlpha) {

    //Create a scissor recangle that covers my Actor.
    Rectangle scissors = new Rectangle();
    Rectangle clipBounds = new Rectangle(getX(),getY(),getWidth(),getHeight());
    ScissorStack.calculateScissors(camera, batch.getTransformMatrix(), clipBounds, scissors);
    batch.flush(); //Make sure nothing is clipped before we want it to.
    ScissorStack.pushScissors(scissors);

    //Draw the actor as usual
    super.draw(batch, parentAlpha);

    //Perform the actual clipping
    ScissorStack.popScissors();
}

这篇关于使 Group 将 Actors 隐藏在其边界之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆