我正计划开发一款回合制游戏,并试图了解如何与 Game Center 通信以及发送和接收马赫数数据。我已经阅读了它并测试了几天,但无法让它按计划工作。
我尝试用下面的代码做的唯一一件事就是能够保存然后读取马赫数据。我正在使用两个沙盒游戏中心帐户进行回合。
轮流通过按“endTurn”按钮发送相同的数据。每次我运行实际用户都经过身份验证并且应用程序设置正确(我相信)。
这是一个测试应用程序,除了测试我所说的之外没有任何其他目的。下面是我用于匹配数据处理的代码。
我真的很感激任何关于我可能做错的想法和提示。在我开始认真测试之前,我确实发布了一个类似的问题,但这并没有解决这个问题,https://stackoverflow.com/questions/14447392/start-gamecenter-turn-based-match-and-initiate-match-data-for -非常第一次。
我也尝试捕捉参与者但没有成功,这可能意味着它是处理完成处理程序时的问题。
-(IBAction)endTurn:(id)sender {
[_gameDictionary setObject:@"The Object" forKey:@"The Key"];
NSLog(@"_gameDictionary: %@", _gameDictionary);
NSData *data = [NSPropertyListSerialization dataFromPropertyList:_gameDictionary format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];
GKTurnBasedParticipant *nextPlayer;
if (_match.currentParticipant == [_match.participants objectAtIndex:0]) {
nextPlayer = [[_match participants] lastObject];
} else {
nextPlayer = [[_match participants]objectAtIndex:0];
}
NSLog(@"_match.currentParticipant: %@", _match.currentParticipant);
[self.match endTurnWithNextParticipant:nextPlayer matchData:data completionHandler:^(NSError *error) {
if (error) {
NSLog(@"An error occured updating turn: %@", [error localizedDescription]);
}
[self.navigationController popViewControllerAnimated:YES];
}];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
_gameDictionary = [[NSMutableDictionary alloc]init];
[self.match loadMatchDataWithCompletionHandler:^(NSData *matchData, NSError *error) {
NSDictionary *myDict = [NSPropertyListSerialization propertyListFromData:_match.matchData mutabilityOption:NSPropertyListImmutable format:nil errorDescription:nil];
[_gameDictionary addEntriesFromDictionary: myDict];
if (error) {
NSLog(@"loadMatchData - %@", [error localizedDescription]);
}
}];
NSLog(@"_gameDictionary: %@", _gameDictionary);
}
输出:
"gk-cdx" = "17.173.254.218:4398";
"gk-commnat-cohort" = "17.173.254.220:16386";
"gk-commnat-main0" = "17.173.254.219:16384";
"gk-commnat-main1" = "17.173.254.219:16385";
}
2013-02-11 22:44:11.707 GC_test1[8791:14f03] _gameDictionary: {
}
2013-02-11 22:44:13.894 GC_test1[8791:14f03] _gameDictionary: {
The Object = The Key;
}
2013-02-11 22:44:13.894 GC_test1[8791:14f03] _match.currentParticipant: (null)