Game Development Reference
In-Depth Information
-(void) removePopover;
@end
Declaring methods in the @interface section of a class allows these methods to be
called from other classes without raising a compiler warning.
Now add the showPopoverNamed: method in Listing 6-7 to GameScene.m . Add it
at the very bottom of the file, just above the @end line.
Listing 6-7 . Showing a popover menu
-(void) showPopoverNamed:(NSString*)name
{
if (_popoverMenuLayer == nil)
{
GameMenuLayer* newMenuLayer
= (GameMenuLayer*)[CCBReader load:name];
[self addChild:newMenuLayer];
_popoverMenuLayer = newMenuLayer;
_popoverMenuLayer.gameScene = self;
_gameMenuLayer.visible = NO;
_levelNode.paused = YES;
}
}
Checking that _popoverMenuLayer is nil ensures there can be only one visible pop-
over menu at a time, even if the method runs twice in succession by accident. Then the
CCBReader load: method is used to load the CCB by its name. The return value is
cast to GameMenuLayer* , which assumes that all popover menus use GameMenuLay-
er as their custom class. The newMenuLayer is then added as a child before it is as-
signed to the ivar _popoverMenuLayer .
You may wonder, “Why not assign directly to _popoverMenuLayer and remove the
local variable newMenuLayer ?” The reason is that _popoverMenuLayer is declared
__weak , so it will not retain any references assigned to it. But when CCBReader
load: returns, there is, if only for a short time, no strong reference holding on to the re-
turned node. Therefore, ARC is going to release the node and set the _popover-
MenuLayer reference to nil even before the next line with addChild: runs! To be
Search WWH ::




Custom Search