2

我正在做一个基于回合的游戏......传输的唯一信息是玩家的分数以及是否已发送回合。

当下一位玩家获得回合时。数据被存储到“scoreToBeat”并且turnSent=1。然后玩家轮到他们。之后游戏结束被调用,因为 turnSent=1。我使用了 Ray Wenderlich 在http://www.raywenderlich.com/5480/beginning-turn-based-gaming-with-ios-5-part-1上的回合制教程。

在他的情况下,比赛结束并且是平局。像这样......我似乎无法让它向失去的人展示。

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

我似乎无法让它显示输掉它的人总是显示出胜利。这是我最近的许多尝试......顺便说一句,比赛中有2名球员......任何想法将不胜感激。

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

           }
4

2 回答 2

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

       } ];
}
于 2012-05-09T20:17:10.823 回答
2

这是我最新项目的摘录,适合 2 人游戏。它在 sendTurn 过程中被调用,当游戏被决定结束时。在我看来,这比上一个答案(第一个代码块)更正确,因为您必须在结束游戏之前为所有参与者设置 matchOutcome。

如果您有超过 2 个玩家,那么您必须遍历所有参与者并相应地设置 matchOutcome。

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

请注意,您将根据设计在两个设备上看到“Won”。

一个人会说“我”,下面有“赢”,另一个人会说“(获奖者姓名)”,下面有“赢”。

于 2012-05-22T22:18:46.193 回答