Game Development Reference
In-Depth Information
Tip It's worth mentioning that I've started using enum values for tags, like
LayerTagGameLayer . Doing so has the advantage over using numbers in
that you can actually read whose tag it is instead of having to remember which
layer had tag 7 assigned to it. It also shows that the actual tag values aren't im-
portant—what's important is that you use the same value consistently for the
same node. Using a human-readable tag makes that task easier and less error
prone. The same goes for action tags, of course.
You may have noticed the variable multiLayerSceneInstance and that it gets
self assigned. A bit strange, isn't it? What would that be good for? Recall how to
create a singleton class from Chapter 3 . In this case, I'll turn the MultiLayerScene
class into a singleton by giving other classes the ability to access the current instance
via a class method. See Listing 5-5 and, if you want, compare it with Listing 3-1 to
spot the differences. In this case the class is already initialized—not initialized on de-
mand within the sharedLayer method.
Listing 5-5. Turning the MultiLayerScene into a Semi-Singleton Object
static MultiLayerScene* sharedMultiLayerScene;
+(MultiLayerScene*) sharedLayer
NSAssert(sharedMultiLayerScene! = nil, @"MultiLayerScene not available!");
return sharedMultiLayerScene;
-(void) dealloc
// MultiLayerScene will be deallocated now, you must set it to nil manually
sharedMultiLayerScene = nil;
Simply put, the sharedMultiLayerScene is a static global variable that will
hold the current MultiLayerScene object during its lifetime. The static
keyword denotes that the sharedMultiLayerScene variable is accessible only
within the implementation file it's defined in. At the same time, it's not an instance
Search Nedrilad ::

Custom Search