0
votes

I'm developing a game with Xcode using Cocos2d 2.0fc0 and I've gotten hung up. I'd like to set a BOOL property when loading the CCLayer/CCScene Game class to define whether second player is local or a remote (Game Center) player. Problem is I'm not exactly sure how to do that under the circumstances. Here is what I've got so far:

@interface RemoteGame : CCLayer <GameCenterControllerDelegate> {
BOOL isRemote;
}

@property (assign, readwrite) BOOL isRemote;

+(CCScene *) sceneIsRemote: (BOOL) b;

-

@implementation RemoteGame

@synthesize isRemote;

+(CCScene *) sceneIsRemote: (BOOL) b {
CCScene *scene = [CCScene node];

    RemoteGame *layer = [RemoteGame node];
    layer.isRemote = b;
    [scene addChild: layer];

    return scene;
}


-(id) init {
    if((self=[super init])) {
        if (isRemote) {
            [GameCenterController sharedController].delegate = self;
        }
    }
}

Then I'm loading the scene like so:

CCScene *trans = [CCTransitionFlipX transitionWithDuration:.75 scene:[RemoteGame sceneIsRemote: YES]];
[[CCDirector sharedDirector] replaceScene:trans];
[self presentGCTurnViewController];

The scene loads fine, but the BOOL is not getting set correctly.

1

1 Answers

1
votes

In shown code [RemoteGame init] is called before layer.isRemote = b;.

[RemoteGame node] calls init method internally, and init checks isRemote flag (which is set to NO by default. When init completes you call layer.isRemote = b; but this does not set delegate. So you need to move [GameCenterController sharedController].delegate = self; into setIsRemote setter.

In general, you can easily check when isRemote flag is set by using "Watch variable" command in debugger.