Yeah I understand that. It's just the docs make it seem that the add
function will add your sprite to whatever your sprite.layer attribute is if
you don't specify a karg layer. But that's not true. If you then use
change_layer you end up doing twice as much work since with 'add' and
'change_layer' it's doing a bisect search each time.

The point I'm making is that yes, '_layer' is an internal variable but
'layer' is never used. If you initialize a sprite and you want it to be
added correctly then you have to set '_layer' before adding. Yes, don't
change the '_layer' attribute after adding.

If we wanted to keep '_layer' strictly an internal variable then we need to
add a bit that gets the 'layer' attribute as it says it will in the docs.
Below it looks for a karg layer, '_layer' and then 'layer' before using the
default.

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

I hope that clarifies my point.
Jeffrey


On Wed, Mar 19, 2014 at 3:48 PM, DR0ID <dr...@bluewin.ch> wrote:

> 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
>



-- 

      Jeffrey Kleykamp

Reply via email to