简体   繁体   中英

GKTurnBasedMatch: Trouble Getting/Setting the matchOutcome

I am working on a Game Center based board game. All seems to be running grew with a few bugs and quirks here and there. The most annoying of those quirks is that when viewing a match that has ended, it always seems to think that the viewing player has lost.

When I end a match with a game winning move, I run this:

// Act if the game is over because of that move
if ([board playerHasWon:activePlayer]) {
    board.canMove = NO;
    match = [CNFTurnBasedMatchHelper sharedInstance].currentMatch;
    NSUInteger currentIndex = [match.participants indexOfObject:match.currentParticipant];
    NSUInteger nextIndex = (currentIndex == 0 ? 1 : 0);
    GKTurnBasedParticipant *nextParticipant = [match.participants objectAtIndex:nextIndex];
    match.currentParticipant.matchOutcome = GKTurnBasedMatchOutcomeWon;
    nextParticipant.matchOutcome = GKTurnBasedMatchOutcomeLost;
    [match endMatchInTurnWithMatchData:[[board stringValue] dataUsingEncoding:NSUTF8StringEncoding] completionHandler:nil];
}

However, I suspect the real problem lies in my retrieval of that matchOutcome.

When loading a match I run this if the it's not the current players turn:

if (match.status == GKTurnBasedMatchStatusEnded) {
    // These lines get the board to show the winning line
    [board playerHasWon:1];
    [board playerHasWon:2];
    board.canMove = NO;
    if (match.currentParticipant.matchOutcome == GKTurnBasedMatchOutcomeWon) statusLabel.text = @"You won!";
    else statusLabel.text = @"You lost!";
}

Well, I solved my own problem just by really laying it out.

if ([((GKTurnBasedParticipant*)[match.participants objectAtIndex:0]).playerID isEqualToString:[GKLocalPlayer localPlayer].playerID]) {
    if ([board playerHasWon:1]) statusLabel.text = @"You won!";
    else statusLabel.text = @"You lost!";
}
else {
    if ([board playerHasWon:2]) statusLabel.text = @"You won!";
    else statusLabel.text = @"You lost!";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM