Game Center Outcome Posting

人走茶凉 提交于 2019-12-14 03:58:52

问题


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;
               }

           }

回答1:


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.




回答2:


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];
           }

       } ];
}


来源:https://stackoverflow.com/questions/10521778/game-center-outcome-posting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!