On Sun, Jan 29, 2012 at 5:03 PM, claudio canepa <[email protected]> wrote:

> The easy but very limited use case is when the layer(s) you want to fade are
> the first to be draw by the visit method; then you can do:
>
> layer_to_fade -> the one you want to fade
>
> fadeable = ColorLayer(0,0,0,255)
> fadeable.add(layer_to_fade, z=-1)
>
> Now fadeable has the attrib opacity, thus working with FadeIn - FadeOut
> (the roles In - Out are reversed when looking from the layer_to_fade POV)
> What we are doing here is painting with black and some alpha over the layer
> to fade (yes, yes, its crude)

Ok, I can not do exactly what you suggested, but you gave me the idea
to solve the problem.

What I did is:

First, I created a Layer that put the image in a Sprite (I previously
had it as an 'image' and just blitted it), and then set a layer's
property to change opacity of the sprite:

class BackgroundLayer(cocos.layer.Layer):
    """Background layer for all the game."""

    def __init__(self, fname):
        super(BackgroundLayer, self).__init__()
        self._image = sp = cocos.sprite.Sprite(fname)
        win_w, win_h = director.get_window_size()
        sp.scale = win_w / sp.width
        sp.position = win_w//2, win_h//2
        self.add(sp)

    def _set_opacity(self, val):
        """Set opacity to the image."""
        self._image.opacity = val

    opacity = property(fset=_set_opacity)


Second, I just put the image as a background, added a lot of middle
layers with stuff that "should appear slowly over the background
image", and finally added on top the same image as background, but
with a fade out:

        background_image = ... # choose an image randomly
        sc = scene.Scene(BackgroundLayer(background_image))
        ...
        # lot of middle layers that I don't care if have opacity or not
        ...
        disap = BackgroundLayer(background_image)
        sc.add(disap, z=10)
        disap.do(actions.FadeOut(3) + actions.CallFunc(sc.remove, disap))


It works very nice!

Thanks for the help! Regards,

-- 
.    Facundo

Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/

-- 
You received this message because you are subscribed to the Google Groups 
"cocos2d discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cocos-discuss?hl=en.

Reply via email to