-
Improving CCMenuPopup
In chapter 2.3 of Building and Monetizing Game Apps for iOS, Todd Perkins demonstrates a method for displaying a popup menu to handle pausing the game. The popup is dismissed by overriding the ccTouchBegan method in the CCMenu subclass:
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
if (![super itemForTouch:touch])
{
return NO;
}
NSArray *ancestors = [NSArray arrayWithObjects:self.parent,
self.parent.parent,
self.parent.parent.parent,
nil];
for (CCNode *n in ancestors)
{
if ([n isKindOfClass:[PopUp class]])
{
[(PopUp *)n closePopUp];
}
}
return [super ccTouchBegan:touch withEvent:event];
}This has the immediate effect of dismissing the popup when one of the menu items is touched. But with the popup and its menu removed from memory, the CCMenuItem for unpausing the game may not get the ccTouchEnded message in time before it is released. Without that message, the game is left is a paused state.
A better solution would be to replace the ccTouchBegin method with a ccTouchEnded method:
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
[super ccTouchEnded:touch withEvent:event];
if ([super itemForTouch:touch])
{
// Simplified
[(PopUp *)self.parent.parent closePopUp];
}
}Using this method, the action on menu item is properly handled before the popup is removed from memory.