0

为了弄清楚选择了哪个级别,我想将选择的按钮信息共享给替换的场景。我该怎么做?

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]];
}
4

3 回答 3

0

标记您的项目,然后将标记设置为您的 GameLayer

像这样的东西......

titem.tag = 777;

...

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

    [[CCDirector sharedDirector] replaceScene: gameLayer];
} 
于 2012-12-17T14:14:07.600 回答
0

为您的场景创建单独的初始化程序。或者至少一个带有枚举:

[[GameLayer alloc] initWithButton:kSceneLoaderButton];
于 2012-12-17T23:18:52.130 回答
-1

对于那种操作,我使用单例

文件.h

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

文件.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]

希望对您有所帮助。

于 2012-12-17T20:50:06.023 回答