paranitips

Never stop learning! がモットーのゆるふわエンジニアブログ

Game CenterのLeaderboardで小数(float型)のスコアを送信する

Game CenterのLeaderboardで小数を使ったスコア(例:12.3m, 123.45ptなど)を使用している場合、スコアの送信方法に少し戸惑ったのでメモします。

まず、iTunes ConnectにてLeaderboardの設定を確認します。

Score Format Type」が

  1. Fixed Point - To 1 Decimal の場合、スコアに10をかけた値を送信する
  2. Fixed Point - To 2 Decimalsの場合、スコアに100をかけた値を送信する
  3. Fixed Point - To 3 Decimalsの場合、スコアに1000をかけた値を送信する

つまり、小数のスコアを整数に修正して送信することでGame Center側で勝手にフォーマットしてくれるよ、ということでした。

コードで書くとこんな感じです。

  GKScore *scoreReporter = [[GKScore alloc] initWithCategory:@"LEADER_BOARD_ID"];
  // Game Centerに送信する値(Fixed Point - To 2 Decimalsの場合)
  int64_t lbScore = (int64_t)(score * 10.0f);
  scoreReporter.value = lbScore;
  [scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
    if (error) {
      // エラーの処理
      NSLog(@"Error : %@", error);
    }
    else {
      // 送信成功
      NSLog(@"Sent Score.");
    }
  }];

参考