I am unsure on which approach will give me a better performance:
I have a GameScene class in which I tend to allocate a objects and then add them as childs to the class with a tag. This approach is used in many examples I studied but I am not sure if it is the best when I need to frequently access to child objects (e.g. update a score label in the GameScene). In those cases I am wondering if it would be a better approach in terms of performance to declare a pointer to those frequently used object and use it to access the object instead of getting the child by tag.
Is it more efficient to use getChildByTag or accessing those objects via their pointer?
As example I provide a label that I frequently update during the game, e.g. it could be the score label:
@interface GameScene : CCLayer
{
CCLabelTTF *frequentlyUsedLabel;
}
//Implementation
-(id) initWithId:(int)sceneId
{
if ((self = [super init]))
{
//..code
frequentlyUsedLabel = [CCLabelTTF labelWithString:@"Hearths:" fontName:@"Arial" fontSize:20];
[self addChild:frequentlyUsedLabel];
}
}
Time how long it takes to render a frame using both methods. Ask yourself if it matters.
The extra memory overhead is a little more difficult to measure (in particular, Cocos2D might be using a clever allocator that is more efficient if your CCLayer subclass doesn’t declare any extra ivars). My take is that 4 bytes is the amount of memory used by a single pixel.
With UIKit, I personally find it much easier to use ivars than to worry about tag uniqueness (especially when reusing code in different apps or using a view in multiple places). This is especially the case with ARC, since you no longer have to worry about getters/setters/retain/release.