【问题标题】:Changing the visibility of a sprite without changing it's position in the sprite group更改精灵的可见性而不改变它在精灵组中的位置
【发布时间】:2020-08-24 22:00:14
【问题描述】:

我有两个精灵组:visible_ui_elementsui_elements。只有visible_ui_elements 中的精灵才会被渲染。我正在使用set_visible 方法来更改精灵的可见性。

class UIMain:

    ui_elements = pg.sprite.Group()
    visible_ui_elements = pg.sprite.Group()


class UIComponent(pg.sprite.Sprite):

   def __init__(self):
       pg.sprite.Sprite.__init__(self)
 
       UIMain.visible_ui_elements.add(self)
       UIMain.ui_elements.add(self)

    def set_visible(self, visibility=False):
        """
        Changes the visibility of this component

        :param visibility: Set to true to make this component visible
        :return: None
        """
        if visibility:
            UIMain.visible_ui_elements.add(self)
        else:
            UIMain.visible_ui_elements.remove(self)

我遇到的问题是,如果一个精灵变得不可见,但后来又变得可见,它在精灵组中的位置被放在该组的前面,而不是它之前的位置。这显然会导致精灵的分层不正确。

【问题讨论】:

    标签: python pygame sprite


    【解决方案1】:

    您可以尝试使用管理分层绘图的精灵组。请参阅pygame.sprite.LayeredUpdates here 的文档。

    添加顺序不仅仅与精灵所在的图层有关。您可以更改图层以按照绘制顺序将它们向上或向下移动,以便您想要的图层位于顶部。

    那时,您将它们添加到组中的顺序无关紧要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      • 2012-02-10
      相关资源
      最近更新 更多