覆盖图纸顺序Scene2D的舞台 [英] overriding drawing order scene2D's stage

查看:105
本文介绍了覆盖图纸顺序Scene2D的舞台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

libgdx中为您提供了一个复杂的Scene2D图,其中包含多个Group'sActor's.您希望用户选择一些Actors并在其末尾绘制,以便它们集中显示在其他任何Actors的上方.

You are given a complex Scene2D graph in libgdx with several Group's and Actor's. You want the user to select some Actors and draw those at the end so they appear focussed on top of any other Actors.

我想遍历Stage两次.第一次绘制未选择的Actors,第二次绘制未选择的actors.但是,我认为没有任何好的"方法可以强制执行此行为.

I would like to iterate over the Stage twice. The first time it draws the unselected Actors, the second time it draws the selected actors. I don't see any 'good' way to enforce this behaviour however.

我希望使用干净的选项.我不想仅仅为了这个小的补充而复制整个方法的实现.

I would prefer options that are clean. I would not like to copy an entire method implementation just for the sake of this small addition.

什么不起作用:

  • Actor's toFront()方法仅适用于其同级对象.
  • 在舞台中交换Actor's的位置:这修改了Actors的转换.
  • the Actor's toFront() method only works for it's siblings.
  • swapping places of Actor's in the Stage: this modifies the transformations the Actors have.

要考虑的场景:您有一个Root,其中有一个组 gA 和一个组 gB . Group gA 包含两个图像 iA1 iA2 . Group gB 包含一张图像 iB .假设首先将Group gA 添加到舞台,并且图像 iA1 iB 重叠;现在,您要选择 iA1 并使其显示在 iB 上.我不想只打电话给 gA.toFront();这会将整个Group gA 放在最前面,这意味着 iA2 也放在最前面.将 iA2 放在前面会产生不希望有的效果,将部分图像隐藏在Group gB

Scenario to think about: you have a Root with a group gA and a group gB. Group gA contains two images iA1 and iA2. Group gB contains one image iB. Given that Group gA is added first to the stage and that image iA1 overlaps with iB; now you want to select iA1 and make it appear over iB. I don't want to only call gA.toFront(); this would put the whole Group gA to the front, which means also iA2 is put to the front. Putting iA2 in front has the undesired effect of hiding parts of images inside Group gB

推荐答案

有两种解决方案-

1-停止使用[多个]组.很烂,但这可能是更简单的选择.如果查看渲染/绘制的完成方式,则从舞台的根组开始,获取其子级并进行渲染.对于这些孩子中的每一个,如果他们是一个小组,则将其渲染为小组孩子. ZIndex 无非就是组中子级的顺序.如果查看Actor的setZIndex,您会明白为什么toFrontsetZIndex仅影响同级兄弟.

1 - Stop using [multiple] groups. Sucks but this may be the easier option. If you look at how rendering/drawing is done you start at the root Group for a Stage and get its children and render them. For each of those children, if they are a Group then render that Groups children. ZIndex is nothing more than the order of children within a group. If you look at the Actor's setZIndex you can see why toFront or setZIndex only affect siblings.

public void setZIndex (int index) {
    if (index < 0) 
        throw new IllegalArgumentException("ZIndex cannot be < 0.");

    Group parent = this.parent;
    if (parent == null) 
        return;

    Array<Actor> children = parent.getChildren();
    if (children.size == 1) 
        return;

    if (!children.removeValue(this, true)) 
        return;

    if (index >= children.size)
        children.add(this);
    else
        children.insert(index, this);
}

2-唯一的其他选择是更改所有角色的绘制顺序.您必须根据选择的不同顺序扩展Stage并将draw方法替换为draw.您可能必须结合Group.drawChildren方法中的许多功能.

2 - The only other option would be to change the drawing order of all the actors. You'd have to extend Stage and replace the draw method to draw based on a different order of your choosing. You'd probably have to incorporate a lot of the functionality from the Group.drawChildren method.

TLDR;在 LibGDX 中实现事物的方式-组是一层.如果您不希望使用图层,则可以更改组的用途或停止使用组.

TLDR; The way things are implemented in LibGDX - a Group is a layer. If you don't want layers then either change what groups do or stop using groups.

这篇关于覆盖图纸顺序Scene2D的舞台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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