Xcode:如何使用GameCenter结束与赢家和输家的2人回合制多人游戏?

时间:2012-06-19 05:53:02

标签: objective-c xcode gamekit

我在找到这方面的任何信息时遇到了一些麻烦,我遇到的所有代码示例都是基于所有玩家的平局结束。在我的2人回合制游戏中,我希望能够以赢家和输家结束比赛。用下面的代码我写的比赛总是以两个球员相同的结果结束,如果它赢了,那么球员1和2都赢了,如果它失去了球员1和2松散......任何帮助?谢谢。

    if (gameOver == true) {
    if (GameWinner == 0) {
        GKTurnBasedParticipant *player0 = [currentMatch.participants objectAtIndex:0];
        player0.matchOutcome = GKTurnBasedMatchOutcomeWon;
        GKTurnBasedParticipant *player1 = [currentMatch.participants objectAtIndex:1];
        player1.matchOutcome = GKTurnBasedMatchOutcomeLost;

        [currentMatch endMatchInTurnWithMatchData:data completionHandler:^(NSError *error) {
            if (error) {
                NSLog(@"%@", error);
            }
        }];  
        testlabel.text = @"Player 1 Wins!";
    } else if (GameWinner == 1) {
        GKTurnBasedParticipant *player0 = [currentMatch.participants objectAtIndex:0];
        player0.matchOutcome = GKTurnBasedMatchOutcomeLost;
        GKTurnBasedParticipant *player1 = [currentMatch.participants objectAtIndex:1];
        player1.matchOutcome = GKTurnBasedMatchOutcomeWon;

        [currentMatch endMatchInTurnWithMatchData:data completionHandler:^(NSError *error) {
            if (error) {
                NSLog(@"%@", error);
            }
        }];
        testlabel.text = @"Player 2 Wins!";
    } else if (GameWinner == 2) {
        for (GKTurnBasedParticipant *part in currentMatch.participants) {
            part.matchOutcome = GKTurnBasedMatchOutcomeTied;
        }
        [currentMatch endMatchInTurnWithMatchData:data completionHandler:^(NSError *error) {
            if (error) {
                NSLog(@"%@", error);
            }
        }];
        testlabel.text = @"Tie Game!";
    } else {
        testlabel.text = @"Your turn is over.";
    }

1 个答案:

答案 0 :(得分:1)

这听起来类似于this SO问题,请尝试:

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