1

我正在开发一个基于游戏中心的 2 人游戏。游戏玩法是;

1) 玩家 1 发起挑战。
2) 玩家 1 轮到他/她。(回答游戏提出的 5 个问题)
3)玩家 1 发送存储在数组中的 5 个问题以及他/她的分数信息。
4) 玩家 2 收到挑战并接受它。
5) 玩家 2 回答相同的问题并显示两个玩家的结果。
6) 玩家 1 被告知玩家 2 的结果。
7) 比赛结束。

我使用以下代码;

- (IBAction)sendTurn:(id)sender {
GKTurnBasedMatch *currentMatch = [[GCTurnBasedMatchHelper sharedInstance] currentMatch];


NSData *data = [NSKeyedArchiver archivedDataWithRootObject:challengedQuestions]; //sends same same questions answered by player1 to player 2 in an array


NSUInteger currentIndex = [currentMatch.participants indexOfObject:currentMatch.currentParticipant];
GKTurnBasedParticipant *nextParticipant;

NSUInteger nextIndex = (currentIndex + 1) % [currentMatch.participants count];
nextParticipant = [currentMatch.participants objectAtIndex:nextIndex];

for (int i = 0; i < [currentMatch.participants count]; i++) {
    nextParticipant = [currentMatch.participants objectAtIndex:((currentIndex + 1 + i) % [currentMatch.participants count ])];
    if (nextParticipant.matchOutcome != GKTurnBasedMatchOutcomeQuit) {
        NSLog(@"isnt' quit %@", nextParticipant);
        break;
    } else {
        NSLog(@"nex part %@", nextParticipant);
    }
}

if (turnCounter > 0)
{
    for (GKTurnBasedParticipant *part in currentMatch.participants) {
        part.matchOutcome = GKTurnBasedMatchOutcomeTied;
    }
    [currentMatch endMatchInTurnWithMatchData:data completionHandler:^(NSError *error) {
        if (error) {
            NSLog(@"%@", error);
        }
    }];
    statusLabel.text = @"Game has ended";
    LYResultsViewController *controller = [[LYResultsViewController alloc] init];
    [controller setChallenge:self.challenge];
    [self.navigationController pushViewController:controller animated:YES];
}
else {

    [currentMatch endTurnWithNextParticipant:nextParticipant matchData:data completionHandler:^(NSError *error) {
        if (error) {
            NSLog(@"%@", error);
            statusLabel.text = @"Oops, there was a problem.  Try that again.";
        }
        else {
            statusLabel.text = @"Your turn is over.";
            LYResultsViewController *controller = [[LYResultsViewController alloc] init];
            [controller setChallenge:self.challenge];
            [self.navigationController pushViewController:controller animated:YES];
        }
    }];
}
NSLog(@"Send Turn, %@, %@", data, nextParticipant);
turnCounter++;

}

问题是如何将数组和玩家 1 的分数发送到同一个 NSData 变量中?

我如何通知玩家 1 玩家 2 的结果?

请帮忙

4

1 回答 1

0

您必须将您的挑战问题和玩家得分添加到字典中。请记住在您的字典中使用 nscoding 兼容的对象,并使用占位符字典从匹配数据中复制。让我永远意识到我必须将匹配数据复制到不同的字典才能访问它。至于分数结果,您只需将传输的分数分配给标签并显示它。如果您正在谈论推送通知,我建议您阅读http://www.raywenderlich.com/5480/beginning-turn-based-gaming-with-ios-5-part-1以获取有关基本通知的信息。不过,游戏中心可以很好地处理其中的大部分内容。

这是我发送匹配数据以进行视觉指导的一个回合动作的片段:

- (IBAction)sendRequirement:(id)sender
{
_submitReq.enabled = NO; //Button that was pressed

_playersTurn.text = @"Submitting turn...";

GKTurnBasedMatch *currentMatch = [[GCTurnBasedMatchHelper sharedInstance] currentMatch];

NSMutableDictionary *tempLoad = [_turnData mutableCopy]; //NSMutableDictionary that holds a copy of the match data

if ([[tempLoad objectForKey:@"p2Name" ] isEqualToString:@"Player 2"])
    {
    [tempLoad setObject:[_turnData objectForKey:@"p2Name"] forKey:@"p2Name"];
    }

[tempLoad setObject:[tempLoad objectForKey:@"p1Score"] forKey:@"p1Score"];
[tempLoad setObject:[tempLoad objectForKey:@"p2Score"] forKey:@"p2Score"];
[tempLoad setObject:[_turnData objectForKey:@"choice"] forKey:@"choice"];
[tempLoad setObject:_textView.text forKey:@"req"];
[tempLoad setObject:@"Win or Lose" forKey:@"outcome"];
[tempLoad setObject:[NSNumber numberWithInt:2] forKey:@"turn1"];
[tempLoad setObject:[NSNumber numberWithInt:2] forKey:@"turn2"];
[tempLoad setObject:[NSNumber numberWithInt:1] forKey:@"turn3"];
[tempLoad setObject:[NSNumber numberWithInt:2] forKey:@"turn4"];

NSData *testData = [NSPropertyListSerialization dataWithPropertyList:tempLoad
                                                              format:NSPropertyListBinaryFormat_v1_0
                                                             options:0
                                                               error:NULL];

NSUInteger currentIndex = [currentMatch.participants indexOfObject:currentMatch.currentParticipant];

NSMutableArray *nextParticipants = [NSMutableArray array];
for (NSInteger i = 0; i < [currentMatch.participants count]; i++)
    {
    NSInteger indx = (i + currentIndex + 1) % [currentMatch.participants count];
    GKTurnBasedParticipant *participant = [currentMatch.participants objectAtIndex:indx];

    if (participant.matchOutcome == GKTurnBasedMatchOutcomeNone)
        {
        [nextParticipants addObject:participant];
        }
    }

[currentMatch endTurnWithNextParticipants:nextParticipants
                              turnTimeout:4320
                                matchData:testData
                        completionHandler:^(NSError *error) {
                            if (error)
                                {
                                NSLog(@"%@", error);

                                _choiceLabel.text = @"Oops, there was a problem. Try that again.";
                                }
                            else
                                {
                                _playersTurn.text = @"";
                                _choiceLabel.text = @"";

                                [self layoutMatch:currentMatch];

                                _submitReq.enabled = YES;
                                }
                        }];
}

我希望这会有所帮助。抱歉我没有早点找到这个。祝你好运!

于 2014-04-29T02:22:46.553 回答