从Game Center发送和接收数据

时间:2012-06-06 22:45:59

标签: iphone ios ios4 game-center multiplayer

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

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

任何想法?

1 个答案:

答案 0 :(得分:1)

您必须将当前视图控制器分配给匹配委托,否则匹配:didReceiveData:fromPlayer:将无效。