游戏中心 - 轮流发送匹配数据和玩家1的结果

时间:2013-06-18 10:35:12

标签: ios objective-c cocoa-touch game-center gamekit

我正在开发一款基于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的结果?

请帮忙

1 个答案:

答案 0 :(得分: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;
                                }
                        }];
}

我希望有所帮助。对不起,我没有早点发现这个。祝你好运!