我已经制作了一个单例来处理游戏中的菜单更改(在菜单层次结构中导航)。我不知道为什么菜单没有显示。请参阅下面的我的MenuManager类单例。
MenuManager.h:
@interface MenuManager : NSObject
{
MenuLayerTypes currentMenuLayer;
CCLayer* runningLayer;
CCLayer* oldLayer;
}
+(MenuManager*)sharedMenuManager;
-(void)runMenuWithID:(MenuLayerTypes)menuLayerID;
@end
MenuManager.m:
#import "MenuManager.h"
@implementation MenuManager
static MenuManager* _sharedMenuManager = nil;
+(MenuManager*)sharedMenuManager
{
@synchronized([MenuManager class])
{
if (!_sharedMenuManager)
[[self alloc] init];
return _sharedMenuManager;
}
return nil;
}
+(id)alloc
{
@synchronized ([MenuManager class])
{
NSAssert(_sharedMenuManager == nil,
@"Attempted to allocated a second instance of the Menu Manager singleton");
_sharedMenuManager = [super alloc];
return _sharedMenuManager;
}
return nil;
}
-(id)init
{
self = [super init];
if (self != nil)
{
Layer = kMainMenuLayer; // default menu type
}
return self;
}
-(void)runMenuWithID:(MenuLayerTypes)menuLayerID {
//MenuLayerTypes oldMenuLayer = currentMenuLayer;
currentMenuLayer = menuLayerID;
oldLayer.tag = 321;
switch (menuLayerID)
{
case kMainMenuLayer:
CCLOG(@"main menu layer");
runningLayer = [MainMenuLayer node];
oldLayer = runningLayer;
break;
case kSettingsLayer:
runningLayer = [SettingsLayer node];
oldLayer = runningLayer;
break;
case kAckLayer:
runningLayer = [AckLayer node];
oldLayer = runningLayer;
break;
default:
CCLOG(@"Unknown ID");
return;
break;
}
MainMenuScene* mainMenuScene = (MainMenuScene *)[[CCDirector sharedDirector] runningScene];
// checks to see if a menu layer is already displayed before replacing it
if (![mainMenuScene getChildByTag:321]) {
[mainMenuScene addChild:runningLayer];
}
else {
[mainMenuScene removeChildByTag:321];
[mainMenuScene addChild:runningLayer];
}
}
@end
同样在MainMenuScene的init中:
[[MenuManager sharedMenuManager] runMenuWithID:kMainMenuLayer];
如何解决此显示问题?