0

I am attempting to set up multiplayer support via the following tutorial (may not be necessary to follow this link to answer this): http://www.raywenderlich.com/60980/game-center-tutorial-how-to-make-a-simple-multiplayer-game-with-sprite-kit-part-1

I have managed to get the demo for this tutorial to work, but I have not been able to get it to work with my game. I have found that the problem lies with the following block of code in 'GameSceneViewController':

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerAuthenticated) name:LocalPlayerIsAuthenticated object:nil];
}

- (void)playerAuthenticated {
    [[GameKitHelper sharedGameKitHelper] findMatchWithMinPlayers:2 maxPlayers:2 viewController:self delegate:self];
}

These methods are in the second view controller, and the player is logged-in in the first view controller. In this block of code, the NSNotification center 'addObserver' code is being called in the first method, but even though the notification has been posted via the previous view controller, the 'playerAuthenticated' method is not being called.

The following code is from the GameKitHelper file which is being used by both view controllers:

- (void)authenticateLocalPlayer
{
    //1
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

    if (localPlayer.isAuthenticated) {
        [[NSNotificationCenter defaultCenter] postNotificationName:LocalPlayerIsAuthenticated object:nil];
        return;
    }
    //2
    localPlayer.authenticateHandler  =
    ^(UIViewController *viewController, NSError *error) {
        //3
        [self setLastError:error];

        if(viewController != nil) {
            //4
            [self setAuthenticationViewController:viewController];
        } else if([GKLocalPlayer localPlayer].isAuthenticated) {
            //5
            _enableGameCenter = YES;
            [[NSNotificationCenter defaultCenter] postNotificationName:LocalPlayerIsAuthenticated object:nil];
        } else {
            //6
            _enableGameCenter = NO;
        }
    };
}

Breakpoints reveal that the second but not the first Notification post is being called, so this should still work since the notification has been posted. This is all code that works well in the demo, but does not work when I have the code in my game. Both are set up for multiplayer in itunes connect, and both have the same gamecenter code.

One major difference between the demo and my game is that the demo calls the first block of code in the first view controller that appears when the game is loaded, and my game calls this instead in a viewController that is navigated to from the start view controller.

I do have a 'dealloc' method which calls 'removeObserver' in the start view controller, and from looking at the SO answer here: NSNotificationCenter selector not being called, it appears this may be removing the notification upon transfer to the other view controller, but I am not totally sure how this dealloc method would be called, and I tried commenting out 'dealloc' in the first view controller, but the same issue still occurs. If you think this I need to be retaining the notification somehow, how would this be done? If you have other ideas as to what's causing this, please help?

Community
  • 1
  • 1
mr_smiggs
  • 59
  • 5
  • Is LocalPlayerIsAuthenticated accessible to both viewController as well as scene? – ZeMoon Oct 03 '14 at 07:22
  • How would I figure this out? I know the notification is being posted in one viewController and attempting to be observed in another. The game scene is being loaded from the game viewController, but the game scene isn't listening for notifications, only the game viewcontroller – mr_smiggs Oct 03 '14 at 17:46
  • @mr_smiggs Was this problem ever solved? I am also stuck at this point. – Robert Farr Dec 08 '14 at 07:13

0 Answers0