iphoneioscocos2d-iphoneatlassprites

what this line means?


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.


Solution

  • 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.