[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] BUG: LayeredUpdate 'layer' vs '_layer'



Am 19.03.2014 07:29, schrieb Jeffrey Kleykamp:
Hi all,

In pygame.sprite.LayeredUpdate.add documentation it says

"If the sprite(s) have an attribute layer then that is used for the layer.
If **kwargs contains 'layer' then the sprite(s) will be added to that
argument (overriding the sprite layer attribute). If neither is passed then
the sprite(s) will be added to the default layer."

This implies it relies on sprite.layer. But by looking at the source I saw
if you want to affect which layer your sprite will get added to, you have
to set sprite._layer before calling add(). So the documentation and the
code doesn't match.

Not so much a bug; more of a clarification.

Sincerely,
Jeffrey



Hi

In my version of sprites.py it looks like the code beneath (it might be outdated). The sprite._layer is an internal variable that should not be used directly (sprite._layer is read only).

If you want to change a layer of a sprite use 'change_layer(self, sprite, new_layer)'. So 'layer' will be handled correctly from the kwargs.

LayeredUpdates:

    def add_internal(self, sprite, layer=None):
        """Do not use this method directly.

        It is used by the group to add a sprite internally.

        """
        self.spritedict[sprite] = Rect(0, 0, 0, 0) # add a old rect

        if layer is None:
            try:
                layer = sprite._layer
            except AttributeError:
                layer = sprite._layer = self._default_layer
        elif hasattr(sprite, '_layer'):
            sprite._layer = layer

        sprites = self._spritelist # speedup
        sprites_layers = self._spritelayers
        sprites_layers[sprite] = layer
        ....


Hope that helps

~DR0ID