【发布时间】:2020-08-24 22:00:14
【问题描述】:
我有两个精灵组:visible_ui_elements 和 ui_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)
我遇到的问题是,如果一个精灵变得不可见,但后来又变得可见,它在精灵组中的位置被放在该组的前面,而不是它之前的位置。这显然会导致精灵的分层不正确。
【问题讨论】: