I have a CCLayer class (yes, it has to be a CCLayer, not CCSprite) that has children CCSprites. I need to change the opacity and the scale of that layer, that in other words is I have to change the opacity and the scale of all children of that layer.
I learnt today that I can extend CClayer class, using this, to deal with the opacity of the children…
#import "CCLayer+Opaque.h"
@implementation CCLayer (CCLayer_Opaque)
// Set the opacity of all of our children that support it
-(void) setOpacity: (GLubyte) opacity
{
for( CCNode *node in [self children] )
{
if( [node conformsToProtocol:@protocol( CCRGBAProtocol)] )
{
[(id<CCRGBAProtocol>) node setOpacity: opacity];
}
}
}
- (GLubyte)opacity {
for (CCNode *node in [self children]) {
if ([node conformsToProtocol:@protocol(CCRGBAProtocol)]) {
return [(id<CCRGBAProtocol>)node opacity];
}
}
return 255;
}
@end
I did that and imported the file in my main class, but I still see this error when I try to set the opacity of that CCLayer: multiple methods named ‘opacity’ found
I also went beyond that and added this to the class extension:
-(void) setScale: (CGFloat) scale
{
for( CCNode *node in [self children] )
{
[node setScale: scale];
}
}
-(CGFloat) scale
{
for( CCNode *node in [self children] )
{
return [node scale];
}
return 1.0f;
}
To deal with the scale, using the same idea, but also see the message multiple methods named ‘scale’ found
In resume: how do I extend the CCLayer class to make all children of a CCLayer change scale or change opacity?
thanks
I discovered the problem. I had to cast the class to the CClayer object in order to read or set the opacity…
Example:
Now it is working. Thanks.