I was reading Learn Cocos2d 2; and stumbled across this problem: in a game scene one might have thousands of entities below the scene hierarchy and all these entities might want to talk to the topmost scene object. The author of Learn Cocos2d 2 suggests using a global variable like this
static GameLayer* sharedGameLayer;
+(GameLayer*) sharedGameLayer
{
NSAssert(sharedGameLayer != nil, @"GameScene instance not yet initialized!");
return sharedGameLayer;
}
+(id) scene
{
CCScene *scene = [CCScene node];
GameLayer *layer = [GameLayer node];
[scene addChild: layer];
InputLayer* inputLayer = [InputLayer node];
[scene addChild:inputLayer z:1 tag:GameSceneLayerTagInput];
return scene;
}
Personally I think this approach unreliable: imagine a player entity want to know the touch input from another layer in the scene, so it calls sharedGameLayer and assuming it is the parent that it actually resides in it just works on from there. But how can it be so sure? what if this sharedGameLayer is another instance that someone else has allocated? Of course there can only be one scene at a time in cocos2d, and so this approach just might work most of the time without any problem. But from a design point of view, I still find such approach lacking.
I’ve thought about two alternatives:
-
using the proper (old) delegate approach. Assigning the correct delegate/delegator relationship at each level. However, this means thousands lines of possibly duplicate code and is not neat from my perspective
-
using NSNotification (or KVO?) to broadcast messages from the children level, and let the parent respond accordingly. But does this mean unnecessary overhead for the system
At this point I’m quite lost, can someone tell me about the general idiom that experienced programmers might use?
You may want to read my “Strategies for accessing other nodes in the hierarchy” guide. Plenty of options to choose from with pros and cons.