@@ -206,7 +206,7 @@ - (NSMutableString *)parseConfigFile:(NSString*)fntFile
206206 element->key = element->fontDef .charID ;
207207 HASH_ADD_INT (_fontDefDictionary, key, element);
208208
209- [validCharsString appendString: [ NSString stringWithFormat: @" %C " , element->fontDef.charID] ];
209+ [validCharsString appendFormat: @" %C " , element->fontDef.charID];
210210 }
211211// else if([line hasPrefix:@"kernings count"]) {
212212// [self parseKerningCapacity:line];
@@ -435,7 +435,11 @@ -(void) setString:(NSString*) newString updateLabel:(BOOL)update;
435435#pragma mark -
436436#pragma mark CCLabelBMFont
437437
438- @implementation CCLabelBMFont
438+ @implementation CCLabelBMFont {
439+ // Replacement for the old CCNode.tag property which was
440+ // used heavily in the original code.
441+ NSMutableArray *_childForTag;
442+ }
439443
440444@synthesize alignment = _alignment;
441445
@@ -526,13 +530,31 @@ -(id) initWithString:(NSString*)theString fntFile:(NSString*)fntFile width:(floa
526530
527531 _reusedChar = [[CCSprite alloc ] initWithTexture: _textureAtlas.texture rect: CGRectMake (0 , 0 , 0 , 0 ) rotated: NO ];
528532 [_reusedChar setBatchNode: self ];
533+ _childForTag = [NSMutableArray array ];
529534
530535 [self setString: theString updateLabel: YES ];
531536 }
532537
533538 return self;
534539}
535540
541+ -(CCSprite *)childForTag : (NSUInteger )tag
542+ {
543+ if (tag < _childForTag.count ){
544+ id child = _childForTag[tag];
545+ return (child == [NSNull null ] ? nil : child);
546+ } else {
547+ return nil ;
548+ }
549+ }
550+
551+ -(void )setTag : (NSUInteger )tag forChild : (CCSprite *)child
552+ {
553+ // Insert NSNull to fill holes if necessary.
554+ while (_childForTag.count < tag) [_childForTag addObject: [NSNull null ]];
555+ [_childForTag addObject: child];
556+ }
557+
536558
537559#pragma mark LabelBMFont - Alignment
538560
@@ -552,9 +574,7 @@ - (void)updateLabel
552574 for (int j = 0 ; j < [_children count ]; j++) {
553575 CCSprite *characterSprite;
554576 int justSkipped = 0 ;
555- int idx = j+skip+justSkipped;
556- NSString * idxStr = [NSString stringWithFormat: @" %d " , idx];
557- while (!(characterSprite = (CCSprite *)[self getChildByName: idxStr recursively: NO ]))
577+ while (!(characterSprite = [self childForTag: j+skip+justSkipped]))
558578 justSkipped++;
559579 skip += justSkipped;
560580
@@ -649,13 +669,7 @@ - (void)updateLabel
649669 continue ;
650670
651671 // Find position of last character on the line
652- CCSprite *lastChar;
653- for (CCSprite* child in [self children ]) {
654- if ([child atlasIndex ]==index) {
655- lastChar = child;
656- break ;
657- }
658- }
672+ CCSprite *lastChar = [self childForTag: index];
659673
660674 lineWidth = lastChar.position .x + lastChar.contentSize .width /2 ;
661675
@@ -678,8 +692,7 @@ - (void)updateLabel
678692 index = i + j + lineNumber;
679693 if (index < 0 )
680694 continue ;
681- NSString * indexStr1 = [NSString stringWithFormat: @" %d " ,(int )index];
682- CCSprite *characterSprite = (CCSprite *)[self getChildByName: indexStr1 recursively: NO ];
695+ CCSprite *characterSprite = [self childForTag: index];
683696 characterSprite.position = ccpAdd (characterSprite.position , ccp (shift, 0 ));
684697 }
685698 }
@@ -774,11 +787,8 @@ -(void) createFontChars
774787 rect.origin .x += _imageOffset.x ;
775788 rect.origin .y += _imageOffset.y ;
776789
777- CCSprite *fontChar;
778-
779790 BOOL hasSprite = YES ;
780- NSString * iStr = [NSString stringWithFormat: @" %d " ,(int )i];
781- fontChar = (CCSprite*) [self getChildByName: iStr recursively: NO ];
791+ CCSprite *fontChar = [self childForTag: i];
782792 if ( fontChar )
783793 {
784794 // Reusing previous Sprite
@@ -797,8 +807,8 @@ -(void) createFontChars
797807 hasSprite = NO ;
798808 } else {
799809 fontChar = [[CCSprite alloc ] initWithTexture: _textureAtlas.texture rect: rect];
800- NSString * iStr1 = [ NSString stringWithFormat: @" %d " ,( int ) i];
801- [self addChild: fontChar z: i name: iStr1 ];
810+ [ self addChild: fontChar z: i];
811+ [self setTag: i forChild: fontChar ];
802812 }
803813
804814 // Apply label properties
0 commit comments