views:

52

answers:

1

I'm trying to make a effect using the CCBitmapFontAtlas, here is what I want:

The string say "ABCDEFG" being dispayed one by one, each one won't be displayed until the one before is completely displayed.

And here is what I tried:

-(id) init

{ if( (self=[super init] )) {

    label = [CCBitmapFontAtlas bitmapFontAtlasWithString:@"ABC" fntFile:@"bitmapFontTest.fnt"];
    [self addChild:label];

    CGSize s = [[CCDirector sharedDirector] winSize];

    label.position = ccp(s.width/2, s.height/2);
    label.anchorPoint = ccp(0.5f, 0.5f);
    label.visible = NO; //hide it first

    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
return self;

}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event

{ CCSprite AChar = (CCSprite) [label getChildByTag:0]; CCSprite BChar = (CCSprite) [label getChildByTag:1]; CCSprite CChar = (CCSprite) [label getChildByTag:2];

id fade_in = [CCFadeIn actionWithDuration:3];

label.visible = YES;

[AChar runAction:fade_in];
[BChar runAction:fade_in];
[CChar runAction:fade_in];

return YES;

}

The effect is the "ABC" will fade in once I touched the screen, then I tried to use the CallFuncND to call the next string to fade in while the current string is displayed. But this seems to make things very complex.

Is there a easier way to get this effect done? Any suggestion will be appreciate.

A: 

I feel like you are going in the right direction with this one. You could have each letter be a separate sprite and store them in an array and then run them each one by one.

The call function can be started by:

[self displayNextSprite:spriteArray nextIndex:0];

And the function is:

 // Warning, This assumes you are not passing it an empty array, you may want to put in a check for that
-(void)displayNextSprite:(NSMutableArray*)spriteArray nextIndex:(NSUInteger)nextIndex
{

   CCSprite *nextSprite = [spriteArray objectAtIndex:nextIndex];

   id action1 = [CCFadeIn actionWithDuration:3];
   // or = [CCPropertyAction actionWithDuration:3 key:@"opacity" from:0 to:255];

   // The last letter
   if(nextIndex == ([spriteArray count] - 1))
   {
      [nextSprite runAction:action1];
   }
   else // continue to the next letter
   {
      id callFunc = [CCCallFunc actionWithTarget:self selector:@selector(displayNextSprite:spriteArray nextIndex:nextIndex+1)];
      id sequence = [CCSequence actionOne:action1 two:callFunc];
      [nextSprite runAction:sequence];
   }
}
Scott Pfeil