objective-cipadcocos2d-iphonefadecclayer

Fading CCLayerColor does not work


I would like to fade a CCLayerColor, which seems to be pretty straightforward, but it just does not work ... Here is what I do :

LoadingScreen.h

@interface LoadingScreen : CCLayerColor{

}

-(id)init;
-(void)setOpacity:(GLubyte)opacity;

@end

LoadingScreen.m

@implementation LoadingScreen

-(id)init{
    if ((self=[super initWithColor:ccc4(66 , 66, 66, 255)])){

        self.isTouchEnabled = NO;

    }
    return self;

}

-(void)setOpacity:(GLubyte)opacity{

    for( CCNode *node in [self children] )
    {
        if( [node conformsToProtocol:@protocol(CCRGBAProtocol)] )
        {
            NSLog(@"conforms to protocol!");
            [(id<CCRGBAProtocol>) node setOpacity: opacity];
        }
    }
}



@end

GameLayer.m

-(id)init{
timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(checkIfLoaded) userInfo:nil repeats:YES];
}


-(void)checkIfLoaded{
    NSLog(@"still doing stuff");
    if(doneInitializing ==YES){
        NSLog(@"done");
        [timer invalidate];
        NSLog(@"FADE NOW !!!!");
        id fade = [CCFadeOut actionWithDuration:2.5];
        [loadingLayer runAction:fade];
        [self performSelector:@selector(removeLoadingLayer) withObject:nil afterDelay:3.5];

    }

    if(loadingScreenPushed == NO && doneInitializing == NO){
        NSLog(@"adding Layer");
        loadingScreenPushed = YES;
        loadingLayer = [LoadingScreen node];
        [self addChild:loadingLayer z:10];

    }



}

Solution

  • As far as I remember, fade actions calculates on each tick new opacity according to the current opacity value. You replace standard CCLayerColor setOpacity method with new one. Try to add

    [super setOpacity: opacity]; 
    

    to your setOpacity: method.