سؤال

In my game players need to log in with Apples Game Center. If they are a new player to the game, they have to be signed in to Game Center first because I use the playerID as part of the data used to setup a new user account.

I have a GameCenter class, which I use to sign the player in, or prompt them to sign in if they are not already, here's the main code.

-(void) setup
{
    gameCenterAuthenticationComplete = NO;

    if (!isGameCenterAPIAvailable()) {
        // Game Center is not available.
        NSLog(@"Game Center is not available.");
    } else {
        NSLog(@"Game Center is available.");

            __weak typeof(self) weakSelf = self; // removes retain cycle error

            GKLocalPlayer *localPlayer =  [GKLocalPlayer localPlayer]; // localPlayer is the public GKLocalPlayer

        __weak GKLocalPlayer *weakPlayer = localPlayer; // removes retain cycle error

            weakPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error)
            {
                if (viewController != nil)
                {
                    [weakSelf showAuthenticationDialogWhenReasonable:viewController];
                }
                else if (weakPlayer.isAuthenticated)
                {
                    [weakSelf authenticatedPlayer:weakPlayer];
                }
                else
                {
                    [weakSelf disableGameCenter];
                }
            };
        }

    }

    -(void)showAuthenticationDialogWhenReasonable:(UIViewController *)controller
    {
        [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:controller animated:YES completion:nil];
    }

    -(void)authenticatedPlayer:(GKLocalPlayer *)player
    {
        NSLog(@"%@,%@,%@",player.playerID,player.displayName, player.alias);

        localPlayer = player;

        gameCenterAuthenticationComplete = YES;       

    }

    -(void)disableGameCenter
    {

    }

And that works fine. But I have 2 issues.

1) As I said if the player is new, then they HAVE to be signed in to Game Center before new user registration can go ahead. This new registration is in another class. So how do I get the other class to listen to the GameCenter's variable gameCenterAuthenticationComplete = YES ? I read elsewhere that I can use a delegate, but does that work across objects? would a notification be better across objects?

2) And more importantly, what happens if the player closes the modal to sign in to GameCenter, where is the call back/block for when the modal is closed? so I can prompt them to sign in again or at least put up a msg on screen?

هل كانت مفيدة؟

المحلول

1).you can access your gameCenterAuthenticationComplete in delegate by importing its class header and declaring an instance of that class in delegate's interface(Appdelegate.h) and accessing gameCenterAuthenticationComplete through that instance in Appdelegate.m OR you can access it by sharedinstance of class do the following in your class Yourclass.h file

+(YourClass*) sharedInstance;

and in YourClass.m

static YourClass* instance;

+(YourClass*) sharedInstance
{
@synchronized(self)
{
    if (instance == nil) 
    {
        instance = [[YourClass alloc] init];
    }
}

return instance;
}

2)this method get called on cancel button of gamecenterleaderboardviewcontroller

- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)view_controller
{
[self.gameCenter dismissModalViewControllerAnimated:NO];
[view_controller release];

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top