在Cocos2D中替换场景时共享菜单信息(按钮信息)

时间:2012-12-17 14:05:15

标签: ios cocos2d-iphone

为了弄清楚选择了什么级别,我想将选定的按钮信息分享给被替换的场景..我该怎么做?

CCLabelBMFont *label = [CCLabelBMFont labelWithString:@"Lavel_2" fntFile:@"font.fnt"];
        CCMenuItemLabel *titem = [CCMenuItemLabel itemWithLabel:label target:self selector:@selector(sceneLoader:)];
        CCMenu *menu = [CCMenu menuWithItems: titem, nil];
        menu.position = ccp(screenSize.width/2, screenSize.height/2);
        [self addChild:menu];


- (void)sceneLoader:(id)sender {
    [[CCDirector sharedDirector] replaceScene: [GameLayer scene]];
}

3 个答案:

答案 0 :(得分:0)

标记您的商品,然后将代码设置为GameLayer

像这样......

titem.tag = 777;

...

- (void)sceneLoader:(CCNode)sender {
    GameLayer *gameLayer = [GameLayer scene];
    gameLayer.gameLevel = sender.tag;

    [[CCDirector sharedDirector] replaceScene: gameLayer];
} 

答案 1 :(得分:0)

为场景创建单独的初始值设定项。或者至少有一个带枚举:

[[GameLayer alloc] initWithButton:kSceneLoaderButton];

答案 2 :(得分:-1)

对于那种操作,我使用单身人士

file.h

@interface YourClassName : NSObject
+ (id)sharedInstance;
@property (nonatomic,retain) NSString * selectedLevel;

file.m

@implementation YourClassName
@synthesize selectedLevel;
+ (id)sharedInstance {
    static YourClassName *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (id)init
{
    self = [super init];
    if (self) {
        selectedLevel = [NSString string];
    }
    return self;
}

如果您想使用它,只需致电[[YourClassName sharedInstance] selectedLevel或设置[[YourClassName] setSelectedLevel]

希望能帮到你。