Game Center Outcome Posting

2019-08-01 05:14发布

I’m doing a turn based game... the only information transferred is the score of the player and whether a turn has been sent.

When the next player receives the turn. The data gets stored into “scoreToBeat” and turnSent=1. The player then takes their turn. Afterward the game ended gets called because turnSent=1. I used the turn based tutorial by Ray Wenderlich at http://www.raywenderlich.com/5480/beginning-turn-based-gaming-with-ios-5-part-1.

In his case the game ends and is a tie. Like this... I can’t seem to get it to show the person that lost.

   for (GKTurnBasedParticipant *part in currentMatch.participants) {           
          part.matchOutcome = GKTurnBasedMatchOutcomeTied;
           }

I can’t seem to get it to show the person that lost it always shows a win. This is my latest attempt of many... Exactly 2 players in the match btw... Any ideas would be greatly appreciated.

    for (GKTurnBasedParticipant *part in currentMatch.participants) {           
           if(part==currentMatch.currentParticipant)
           {
               if(points>scoreToBeat)
               {
                   part.matchOutcome=GKTurnBasedMatchOutcomeWon;   
               }
               else {
                   part.matchOutcome=GKTurnBasedMatchOutcomeLost;
               }
           }
           else {
               if(points>scoreToBeat)
               {
                   part.matchOutcome=GKTurnBasedMatchOutcomeLost;   
               }
               else {
                   part.matchOutcome=GKTurnBasedMatchOutcomeWon;
               }

           }

2条回答
smile是对你的礼貌
2楼-- · 2019-08-01 05:25

This is an excerpt from my latest project, good for a 2 player game. It's called during the sendTurn process, when the game is decided to be ending. This is more correct than the previous answer (first code block), in my opinion, because you must set the matchOutcome for all participants before ending the game.

If you had more than 2 players, then you'd have to loop through all participants and set the matchOutcome accordingly.

   GKTurnBasedParticipant *curr = currentMatch.currentParticipant;
    NSUInteger currentIndex = [currentMatch.participants indexOfObject:currentMatch.currentParticipant];
    NSUInteger nextIndex = (currentIndex + 1) % [currentMatch.participants count];
    GKTurnBasedParticipant *next = [currentMatch.participants objectAtIndex:nextIndex];

    if (currScore < otherScore)
    {
        // Curr player lost
        curr.matchOutcome = GKTurnBasedMatchOutcomeLost;
        next.matchOutcome = GKTurnBasedMatchOutcomeWon;
    }
    else if (currScore == otherScore)
    {
        // Tied
        curr.matchOutcome = GKTurnBasedMatchOutcomeTied;
        next.matchOutcome = GKTurnBasedMatchOutcomeTied;
    }
    else 
    {
        // Won
        curr.matchOutcome = GKTurnBasedMatchOutcomeWon;
        next.matchOutcome = GKTurnBasedMatchOutcomeLost;
    }

Note that you will see "Won" on both devices by design.

One will say "Me" with "Won" underneath, and the other will say "(Winners Name)" with "Won" underneath.

查看更多
太酷不给撩
3楼-- · 2019-08-01 05:28

points is the local points scored on the device right? if so, I would do the following:

  if([part.playerID isEqualToString [GKLocalPlayer localPlayer].playerID]]) {

 if(points>scoreToBeat) {

part.matchOutcome = GKTurnBasedMatchOutComeWon;

} else {

part.matchOutcome = GKTurnBasedMatchOutComeLost;

} 

} else { 
    if(points>scoreToBeat) {
               part.matchOutcome=GKTurnBasedMatchOutcomeLost;   
           } else {
               part.matchOutcome=GKTurnBasedMatchOutcomeWon;
           }

       }
}

Also, remember to use a NSLog to see if score to beat is actually transferred. One mistake you can do to keep on using currentTurnBasedMatch.matchData, you should set the currentTurnBasedMatch to GKTurnBasedMatch that is returned in delegate methods.

EDIT: A code snipped I use looks like this

if([participant.playerID isEqualToString:[GKLocalPlayer localPlayer].playerID]){
        NSLog(@"we are the last player in the game -end the game");
        for (GKTurnBasedParticipant *part in match.participants) {

            part.matchOutcome = GKTurnBasedMatchOutcomeTied;
        }
       [match endMatchInTurnWithMatchData:match.matchData completionHandler:^(NSError *error) {
           if ([AppDelegate mainMenuController].multiGameMenu.turnBasedMenu !=nil) {

               [[AppDelegate mainMenuController].multiGameMenu.turnBasedMenu reloadTableView];
           }

       } ];
}
查看更多
登录 后发表回答