2

私のゲームでは、プレイヤーはApplesGameCenterにログインする必要があります。彼らがゲームの新しいプレーヤーである場合、新しいユーザーアカウントの設定に使用されるデータの一部としてplayerIDを使用するため、最初にGameCenterにサインインする必要があります。

GameCenterクラスがあります。これを使用してプレーヤーにサインインするか、まだサインインしていない場合はサインインするように促します。メインコードは次のとおりです。

-(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
    {

    }

そして、それはうまくいきます。しかし、私には2つの問題があります。

1)プレーヤーが新しい場合は、新しいユーザーの登録を進める前に、GameCenterにサインインする必要があります。この新規登録は別のクラスにあります。では、他のクラスにGameCenterの変数gameCenterAuthenticationComplete = YESをリッスンさせるにはどうすればよいですか?デリゲートを使用できることを他の場所で読みましたが、それはオブジェクト間で機能しますか?通知はオブジェクト間でより適切でしょうか?

2)さらに重要なことに、プレイヤーがモーダルを閉じてGameCenterにサインインするとどうなりますか?モーダルが閉じられたときのコールバック/ブロックはどこにありますか?だから私は彼らに再度サインインするように促すことができますか、少なくとも画面にメッセージを表示することができますか?

4

1 に答える 1

2

1)。デリゲートでgameCenterAuthenticationCompleteにアクセスするには、クラスヘッダーをインポートし、デリゲートのインターフェイス(Appdelegate.h)でそのクラスのインスタンスを宣言し、Appdelegate.mでそのインスタンスを介してgameCenterAuthenticationCompleteにアクセスします。または、クラスdoのsharedinstanceでアクセスできます。クラスYourclass.hファイル内の次の

+(YourClass*) sharedInstance;

そしてYourClass.mで

static YourClass* instance;

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

return instance;
}

2)このメソッドはgamecenterleaderboardviewcontrollerのキャンセルボタンで呼び出されます

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

}
于 2013-03-27T05:50:57.510 に答える