2

我在从游戏中心多人游戏设备之间发送数据时遇到了很多麻烦。我可以建立匹配并让两个用户都连接,但由于某种原因我无法发送数据。这是我的代码:

-(void)sendData {
    NSError *error;
    int myScore = scoreInt;
    NSData *packet = [NSData dataWithBytes:&myScore length:sizeof(myScore)];
    [theMatch sendDataToAllPlayers: packet withDataMode: GKMatchSendDataUnreliable error: &error];
    if (error != nil)
    {
        NSLog(@"ERROR: %@", error);
    }    
}

-(void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID {

    NSLog(@"called");

}

我从另一个角度进行比赛,我不知道这是否是问题,但这里是游戏中心找到比赛时的代码:

 - (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match
{
    MultiplayerView *mpv = [[MultiplayerView alloc] init];

    [self dismissModalViewControllerAnimated:NO];

    mpv.theMatch = match; // Use a retaining property to retain the match.

    match.delegate = self;

    NSLog(@"Matched");
    if (!self.matchStarted && match.expectedPlayerCount == 0)
    {
        self.matchStarted = YES;
        NSLog(@"Lets Go");
        MultiplayerView *mpv = [[MultiplayerView alloc] init];
        [mpv setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [self presentModalViewController:mpv animated:YES];
    }
}

有任何想法吗?

4

1 回答 1

1

您必须将当前视图控制器分配给您的匹配委托,否则 match:didReceiveData:fromPlayer: 将不起作用。

于 2012-10-12T07:54:23.003 回答