@@ -424,39 +424,6 @@ -(void) parseKerningEntry:(NSString*) line
424
424
#pragma mark -
425
425
#pragma mark CCLabelBMFont
426
426
427
- static NSString * stringFromUInt32 (UInt32 i)
428
- {
429
- static NSString * strings[] = { @" 0" , @" 1" , @" 2" , @" 3" , @" 4" , @" 5" , @" 6" , @" 7" , @" 8" , @" 9" ,
430
- @" 10" , @" 11" , @" 12" , @" 13" , @" 14" , @" 15" , @" 16" , @" 17" , @" 18" , @" 19" ,
431
- @" 20" , @" 21" , @" 22" , @" 23" , @" 24" , @" 25" , @" 26" , @" 27" , @" 28" , @" 29" ,
432
- @" 30" , @" 31" , @" 32" , @" 33" , @" 34" , @" 35" , @" 36" , @" 37" , @" 38" , @" 39" ,
433
- @" 40" , @" 41" , @" 42" , @" 43" , @" 44" , @" 45" , @" 46" , @" 47" , @" 48" , @" 49" ,
434
- @" 50" , @" 51" , @" 52" , @" 53" , @" 54" , @" 55" , @" 56" , @" 57" , @" 58" , @" 59" ,
435
- @" 60" , @" 61" , @" 62" , @" 63" , @" 64" , @" 65" , @" 66" , @" 67" , @" 68" , @" 69" ,
436
- @" 70" , @" 71" , @" 72" , @" 73" , @" 74" , @" 75" , @" 76" , @" 77" , @" 78" , @" 79" ,
437
- @" 80" , @" 81" , @" 82" , @" 83" , @" 84" , @" 85" , @" 86" , @" 87" , @" 88" , @" 89" ,
438
- @" 90" , @" 91" , @" 92" , @" 93" , @" 94" , @" 95" , @" 96" , @" 97" , @" 98" , @" 99" ,
439
- @" 100" , @" 101" , @" 102" , @" 103" , @" 104" , @" 105" , @" 106" , @" 107" , @" 108" , @" 109" ,
440
- @" 110" , @" 111" , @" 112" , @" 113" , @" 114" , @" 115" , @" 116" , @" 117" , @" 118" , @" 119" ,
441
- @" 120" , @" 121" , @" 122" , @" 123" , @" 124" , @" 125" , @" 126" , @" 127" , @" 128" , @" 129" ,
442
- @" 130" , @" 131" , @" 132" , @" 133" , @" 134" , @" 135" , @" 136" , @" 137" , @" 138" , @" 139" ,
443
- @" 140" , @" 141" , @" 142" , @" 143" , @" 144" , @" 145" , @" 146" , @" 147" , @" 148" , @" 149" ,
444
- @" 150" , @" 151" , @" 152" , @" 153" , @" 154" , @" 155" , @" 156" , @" 157" , @" 158" , @" 159" ,
445
- @" 160" , @" 161" , @" 162" , @" 163" , @" 164" , @" 165" , @" 166" , @" 167" , @" 168" , @" 169" ,
446
- @" 170" , @" 171" , @" 172" , @" 173" , @" 174" , @" 175" , @" 176" , @" 177" , @" 178" , @" 179" ,
447
- @" 180" , @" 181" , @" 182" , @" 183" , @" 184" , @" 185" , @" 186" , @" 187" , @" 188" , @" 189" ,
448
- @" 190" , @" 191" , @" 192" , @" 193" , @" 194" , @" 195" , @" 196" , @" 197" , @" 198" , @" 199" ,
449
- @" 200" , @" 201" , @" 202" , @" 203" , @" 204" , @" 205" , @" 206" , @" 207" , @" 208" , @" 209" ,
450
- @" 210" , @" 211" , @" 212" , @" 213" , @" 214" , @" 215" , @" 216" , @" 217" , @" 218" , @" 219" ,
451
- @" 220" , @" 221" , @" 222" , @" 223" , @" 224" , @" 225" , @" 226" , @" 227" , @" 228" , @" 229" ,
452
- @" 230" , @" 231" , @" 232" , @" 233" , @" 234" , @" 235" , @" 236" , @" 237" , @" 238" , @" 239" ,
453
- @" 240" , @" 241" , @" 242" , @" 243" , @" 244" , @" 245" , @" 246" , @" 247" , @" 248" , @" 249" ,
454
- @" 250" , @" 251" , @" 252" , @" 253" , @" 254" , @" 255" };
455
-
456
-
457
- return ((i & 0xFFFFFF00 ) ? @(i).stringValue : strings[i]);
458
- }
459
-
460
427
@interface CCLabelBMFont ()
461
428
462
429
-(int ) kerningAmountForFirst : (unichar )first second : (unichar )second ;
@@ -468,7 +435,11 @@ -(void) setString:(NSString*) newString updateLabel:(BOOL)update;
468
435
#pragma mark -
469
436
#pragma mark CCLabelBMFont
470
437
471
- @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
+ }
472
443
473
444
@synthesize alignment = _alignment;
474
445
@@ -559,13 +530,31 @@ -(id) initWithString:(NSString*)theString fntFile:(NSString*)fntFile width:(floa
559
530
560
531
_reusedChar = [[CCSprite alloc ] initWithTexture: _textureAtlas.texture rect: CGRectMake (0 , 0 , 0 , 0 ) rotated: NO ];
561
532
[_reusedChar setBatchNode: self ];
533
+ _childForTag = [NSMutableArray array ];
562
534
563
535
[self setString: theString updateLabel: YES ];
564
536
}
565
537
566
538
return self;
567
539
}
568
540
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
+
569
558
570
559
#pragma mark LabelBMFont - Alignment
571
560
@@ -585,9 +574,7 @@ - (void)updateLabel
585
574
for (int j = 0 ; j < [_children count ]; j++) {
586
575
CCSprite *characterSprite;
587
576
int justSkipped = 0 ;
588
- int idx = j+skip+justSkipped;
589
- NSString * idxStr = stringFromUInt32 (idx);
590
- while (!(characterSprite = (CCSprite *)[self getChildByName: idxStr recursively: NO ]))
577
+ while (!(characterSprite = [self childForTag: j+skip+justSkipped]))
591
578
justSkipped++;
592
579
skip += justSkipped;
593
580
@@ -682,13 +669,7 @@ - (void)updateLabel
682
669
continue ;
683
670
684
671
// Find position of last character on the line
685
- CCSprite *lastChar;
686
- for (CCSprite* child in [self children ]) {
687
- if ([child atlasIndex ]==index) {
688
- lastChar = child;
689
- break ;
690
- }
691
- }
672
+ CCSprite *lastChar = [self childForTag: index];
692
673
693
674
lineWidth = lastChar.position .x + lastChar.contentSize .width /2 ;
694
675
@@ -711,8 +692,7 @@ - (void)updateLabel
711
692
index = i + j + lineNumber;
712
693
if (index < 0 )
713
694
continue ;
714
- NSString * indexStr1 = stringFromUInt32 ((UInt32)index);
715
- CCSprite *characterSprite = (CCSprite *)[self getChildByName: indexStr1 recursively: NO ];
695
+ CCSprite *characterSprite = [self childForTag: index];
716
696
characterSprite.position = ccpAdd (characterSprite.position , ccp (shift, 0 ));
717
697
}
718
698
}
@@ -807,11 +787,8 @@ -(void) createFontChars
807
787
rect.origin .x += _imageOffset.x ;
808
788
rect.origin .y += _imageOffset.y ;
809
789
810
- CCSprite *fontChar;
811
-
812
790
BOOL hasSprite = YES ;
813
- NSString * iStr = stringFromUInt32 ((UInt32)i);
814
- fontChar = (CCSprite*) [self getChildByName: iStr recursively: NO ];
791
+ CCSprite *fontChar = [self childForTag: i];
815
792
if ( fontChar )
816
793
{
817
794
// Reusing previous Sprite
@@ -830,8 +807,8 @@ -(void) createFontChars
830
807
hasSprite = NO ;
831
808
} else {
832
809
fontChar = [[CCSprite alloc ] initWithTexture: _textureAtlas.texture rect: rect];
833
- NSString * iStr1 = stringFromUInt32 ((UInt32)i) ;
834
- [self addChild: fontChar z: i name: iStr1 ];
810
+ [ self addChild: fontChar z: i] ;
811
+ [self setTag: i forChild: fontChar ];
835
812
}
836
813
837
814
// Apply label properties
0 commit comments