2012-06-06 48 views
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]; 
    } 
} 

任何想法?

+0

大衛布拉德我有同樣的問題http://stackoverflow.com/questions/12641113/issues-regarding-gamecenterios。但沒有得到任何answer.please幫助我。如果你得到任何答案關於這個 –

回答

1

您必須將當前視圖控制器分配給您的匹配委託,否則匹配:didReceiveData:fromPlayer:將不起作用。

+0

是的!感謝那。 (如果運行時抱怨一個零委託會很棒) – buildsucceeded