0

i found this line in the init method from an open source project :

AtlasSpriteManager *spriteManager =
    (AtlasSpriteManager*)[self getChildByTag:kSpriteManager];

and kSpriteManager = 0;

then spriteManager used for this purpose

     AtlasSprite *bird = [AtlasSprite spriteWithRect:
                         CGRectMake(608,16,44,32) spriteManager:spriteManager];
    [spriteManager addChild:bird z:4 tag:kBird];

any idea will be great thank you.

e.James
  • 116,942
  • 41
  • 177
  • 214
Bobj-C
  • 5,276
  • 9
  • 47
  • 83

1 Answers1

1

Starting with the first line:

AtlasSpriteManager *spriteManager = 
    (AtlasSpriteManager*)[self getChildByTag:kSpriteManager]; 

This means that there is a method called -getChildByTag: which returns a generic child object. Since the returned object is generic (no specific type) it must be cast to the appropriate type before it can be used. I would guess that the method definition looks something like this:

- (id)getChildByTag:(NSInteger)tag;

Internally, the class would contain an array of generic child objects, and a specific child can be retrieved by calling getChildByTag: with the appropriate tag.

In this case, the programmer knew that the child with tag 0 is an AtlasSpriteManager, so they simply cast to that type and then used the spriteManager as they normally would.

e.James
  • 116,942
  • 41
  • 177
  • 214
  • this is the method '-(CocosNode*) getChildByTag:(int) aTag { NSAssert( aTag != kCocosNodeTagInvalid, @"Invalid tag"); for( CocosNode *node in children ) { if( node.tag == aTag ) return node; } // not found return nil; }' – Bobj-C Aug 04 '11 at 19:44
  • That makes sense. `AtlasSpriteManager` will be derived from `CocosNode`. – e.James Aug 04 '11 at 21:00