在CCBitmapFontAtlas效果上(一个显示)

时间:2010-09-26 12:50:48

标签: cocos2d-iphone

我正在尝试使用CCBitmapFontAtlas创建效果,这是我想要的:

字符串说“ABCDEFG”逐个发送,每个都不会显示 直到前一个完全显示。

以下是我的尝试:

-(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;

}

效果是“ABC”会在我触摸屏幕后淡入,然后我尝试使用了 CallFuncND在显示当前字符串时调用下一个淡入的字符串。 但这似乎使事情变得非常复杂。

有没有更简单的方法来完成这个效果? 任何建议都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

我觉得你正朝着正确的方向前进。你可以让每个字母都是一个单独的精灵,然后将它们存储在一个数组中,然后逐个运行它们。

可以通过以下方式启动呼叫功能:

[self displayNextSprite:spriteArray nextIndex:0];

功能是:

 // 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];
   }
}