I had the same issue which I've 'fixed' like this:
The Layer class itself doesn't support opacity because it's basically a 'see through' container for it's children.
To fade a Layer you really just want to fade all of it's children. You can do this as follows:
On your Layer subclass, add the following:
// Set the opacity of all of our children that support it
-(void) setOpacity: (GLubyte) opacity
{
for( CocosNode *node in [self children] )
{
if( [node conformsToProtocol:@protocol( CocosNodeRGBA)] )
{
[(id<CocosNodeRGBA>) node setOpacity: opacity];
}
}
}
This method forwards the setOpacity call to each child of the Layer to make sure they all fade. The important bit is the check to make sure that the node actually supports setOpacity in the first place.
Generally, any class which supports setting opacity implements the CocosNodeRGBA protocol so this is what we check for.
Note that we could just test to see if the method is supported using:
if( [node respondsToSelector:@selector( setOpacity:)] )
The problem with this is that in the body of the if statement we then try to call setOpacity and the compiler will warn us that this may not be supported. Not a real problem but it's nice to avoid upsetting the compiler :)
Note
This works for me but there is a potential problem. If a child of the node doesn't support setOpacity then it won't be faded so you will have to handle this. The code above doesn't.
One option would be to turn visibility of the child off if opacity is 0 and on if it's not. You could change the method above to implement this :
// Set the opacity of all of our children that support it
-(void) setOpacity: (GLubyte) opacity
{
for( CocosNode *node in [self children] )
{
if( [node conformsToProtocol:@protocol( CocosNodeRGBA)] )
{
[(id<CocosNodeRGBA>) node setOpacity: opacity];
}
// Handle children that don't support opacity
else
{
node.visible = ( opacity != 0 );
}
}
}
This will work but might be a bit of an ugly solution as children will pop in and out if they don't support opacity.
A better solution, and the one I ended up implementing, is to find any children that don't implement opacity (and there aren't that many of them) and subclass them, adding your own setOpacity method as above.
Hope this helps
ScoobaDood
<ShamelessPlug>
Check out Shape Up! at http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=329995992&mt=8
</ShamelessPlug>