Sunday, August 19, 2012

Add Pop Up Message to the Game


#import "cocos2d.h"

@interface Scene4UILayer : CCLayer {
    CCLabelTTF *label;
}

- (BOOL)displayText:(NSString *)text
andOnCompleteCallTarget:(id)target selector:(SEL)selector;

@end

//////////////

#import "Scene4UILayer.h"

@implementation Scene4UILayer

- (id)init {   
    if ((self = [super init])) {       
        CGSize winSize = [CCDirector sharedDirector].winSize;
        label = [CCLabelTTF labelWithString:@"" fontName:@"Helvetica"
                                   fontSize:48.0];
        label.position = ccp(winSize.width/2, winSize.height/2);
        label.visible = NO;
        [self addChild:label];       
    }
    return self;   
}

- (BOOL)displayText:(NSString *)text
andOnCompleteCallTarget:(id)target selector:(SEL)selector {
    [label stopAllActions];
    [label setString:text];
    label.visible = YES;
    label.scale = 0.0;
    label.opacity = 255;
   
    CCScaleTo *scaleUp = [CCScaleTo actionWithDuration:0.5 scale:1.2];
    CCScaleTo *scaleBack =
    [CCScaleTo actionWithDuration:0.1 scale:1.0];
    CCDelayTime *delay = [CCDelayTime actionWithDuration:2.0];
    CCFadeOut *fade = [CCFadeOut actionWithDuration:0.5];
    CCHide *hide = [CCHide action];
    CCCallFuncN *onComplete =
    [CCCallFuncN actionWithTarget:target selector:selector];
    CCSequence *sequence = [CCSequence actions:scaleUp, scaleBack,
                            delay, fade, hide, onComplete, nil];
    [label runAction:sequence];   
    return TRUE;   
}

@end