我知道这个主题已经讨论了好几次,但是我无法弄清楚导致这个错误信息的原因。我已经被困在这里几个星期了,它让我疯狂。该方法的第一部分为2-4名玩家设置了一个匹配。一旦找到匹配,它会尝试在一定限度内添加3个和4个玩家。现在,限制只设置为3秒,但我认为它一旦准备好发布就会大得多。
我知道这是matchmakerViewController代码中某处的内存错误。当我注释掉改变场景的代码时,我仍然得到错误。你能告诉我我做错了什么以及如何解决它?
-(void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match
{
NSLog(@"IN matchmakerViewController!");
NSLog(@"Found a match!");
//startDate = [[NSDate date] retain];
//NSArray * playerIds1 = match.playerIDs;
//[marray_players addObject:playerIds1];
// Setup match
TXGameCenterManager *gameCenterManager = [TXGameCenterManager sharedTXGameCenterManager];
gameCenterManager.multiplayerMatch = match;
// The delegate of the match is HelloWorldLayer
gameCenterManager.multiplayerMatch.delegate = self;
AppDelegate * delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
[delegate.viewController dismissModalViewControllerAnimated:NO];
GKMatchRequest * matchRequest = [[[GKMatchRequest alloc] init] autorelease];
NSLog(@"playerIDs count is %d",[match.playerIDs count] );
if( match.expectedPlayerCount==0 )
{
// Launching the game without waiting for connection change messages
NSLog(@"Begin game without waiting for match connection change messages");
// Determine the host, local or remote
NSArray * playerIds = match.playerIDs;
NSLog(@"Number of players: %d", [playerIds count]);
NSLog(@"ID of player: %@", [playerIds lastObject]);
NSLog(@"I got the player ids");
[GKPlayer loadPlayersForIdentifiers:playerIds withCompletionHandler:^(NSArray *players, NSError * error)
{
NSLog(@"Player 1 %@", [[players objectAtIndex:0] alias] );
// NSLog(@"Player 2 %@", [[players objectAtIndex:1] alias] );
if( !error)
{
NSLog(@"Local player: %@", [[GKLocalPlayer localPlayer] alias]);
NSLog(@"Remote player: %@", [[players lastObject] alias]);
NSLog(@"Remote player: %@", [[players lastObject] alias]);
//NSArray *nsarray_player2 = [marray_players objectAtIndex:1];
// remote1name= [[nsarray_player1 lastObject] alias];
int_totalremoteplayers=[match.playerIDs count];
boo_playersfound=true;
localName = [[GKLocalPlayer localPlayer] alias];
remoteName = [[players objectAtIndex:0] alias];
NSLog(@"Player 1 %@", [[players objectAtIndex:0] alias] );
// NSLog(@"Player 2 %@", [[players objectAtIndex:1] alias] );
//remote1name= [[nsarray_player1 lastObject] alias];
if ([match.playerIDs count]==1)
{
NSLog(@"IN [match.playerIDs count]==1");
remoteplayer1name= [[players objectAtIndex:0] alias];
[playernames addObject:remoteplayer1name];
[playernames addObject:localName];
NSArray *sortedArray;
sortedArray = [playernames sortedArrayUsingSelector:
@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"Host: %@", [sortedArray objectAtIndex:0]);
NSLog(@"Player1: %@", [sortedArray objectAtIndex:1]);
hostName=[sortedArray objectAtIndex:0];
remoteplayer1name=[sortedArray objectAtIndex:1];
if ([hostName isEqualToString:localName])
{
[[TXGameCenterManager sharedTXGameCenterManager] setHost:YES];
}
}
if ([match.playerIDs count]==2)
{
NSLog(@"IN [match.playerIDs count]==2");
NSLog(@" [match.playerIDs count]==2 Player 1 %@", [[players objectAtIndex:0] alias] );
NSLog(@"[match.playerIDs count]==2 Player 2 %@", [[players objectAtIndex:1] alias] );
NSLog(@"[match.playerIDs count]==2 localName %@", localName );
remoteplayer1name= [[players objectAtIndex:0] alias];
remoteplayer2name= [[players objectAtIndex:1] alias];
[playernames addObject:remoteplayer1name];
[playernames addObject:remoteplayer2name];
[playernames addObject:localName];
NSLog(@"Players in playernames %d", [playernames count]);
NSArray *sortedArray;
sortedArray = [playernames sortedArrayUsingSelector:
@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"Host: %@", [sortedArray objectAtIndex:0]);
NSLog(@"Player1: %@", [sortedArray objectAtIndex:1]);
NSLog(@"Player2: %@", [sortedArray objectAtIndex:2]);
hostName=[sortedArray objectAtIndex:0];
remoteplayer1name=[sortedArray objectAtIndex:1];
remoteplayer2name=[sortedArray objectAtIndex:2];
if ([hostName isEqualToString:localName])
{
[[TXGameCenterManager sharedTXGameCenterManager] setHost:YES];
}
NSLog(@"match.expectedPlayerCount = %d",match.expectedPlayerCount);
}
else if ([match.playerIDs count]==3)
{
remoteplayer1name= [[players objectAtIndex:0] alias];
remoteplayer2name= [[players objectAtIndex:1] alias];
remoteplayer3name= [[players objectAtIndex:2] alias];
[playernames addObject:remoteplayer1name];
[playernames addObject:remoteplayer2name];
[playernames addObject:remoteplayer3name];
[playernames addObject:localName];
NSArray *sortedArray;
sortedArray = [playernames sortedArrayUsingSelector:
@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"Host: %@", [sortedArray objectAtIndex:0]);
NSLog(@"Player 1: %@", [sortedArray objectAtIndex:1]);
NSLog(@"Player 2: %@", [sortedArray objectAtIndex:2]);
NSLog(@"Player 3: %@", [sortedArray objectAtIndex:3]);
hostName=[sortedArray objectAtIndex:0];
remoteplayer1name=[sortedArray objectAtIndex:1];
remoteplayer2name=[sortedArray objectAtIndex:2];
remoteplayer3name=[sortedArray objectAtIndex:3];
if ([hostName isEqualToString:localName])
{
[[TXGameCenterManager sharedTXGameCenterManager] setHost:YES];
}
}
NSLog(@"Player names");
// TODO: Set the correct player names in the globals
player1 = localName;
player2 = [[NSUserDefaults standardUserDefaults] objectForKey:@"player2name"];
player3 = [[NSUserDefaults standardUserDefaults] objectForKey:@"player3name"];
player4 = [[NSUserDefaults standardUserDefaults] objectForKey:@"player4name"];
NSLog(@"Launch the game");
// Launch the game
int_team2tally=0;
NSLog(@"boo_startgame");
boo_startgame=true;
NSLog(@"go to the required scene");
//go to the required scene
}
else
{
NSLog(@"Error");
}
NSLog(@"StartMultiplayerGameTablehost");
//gameCenterManager.matchController.matchmakerDelegate = nil;
[self performSelector:@selector(StartMultiplayerGameTablehost) withObject:nil afterDelay:1.];
}
];
NSLog(@"No Error");
}
// Believe the error is somewhere in the following code.
if (boo_gamestarted!=true)
{
if (match.expectedPlayerCount==0)
{
//if([localName isEqualToString:hostName])
// {
boo_startgame=true;
if ([match.playerIDs count]==1)
{
matchRequest.minPlayers = 3;
matchRequest.maxPlayers = 4;
matchRequest.playersToInvite = nil;
[[GKMatchmaker sharedMatchmaker] addPlayersToMatch:match matchRequest:matchRequest completionHandler:^(NSError *error)
{
if (error)
{
NSLog(@"An error occurred during adding a player to match: %@", [error localizedDescription]);}
else if (match != nil)
{NSLog(@"A player has been added to the match");}
}];
}
else if ([match.playerIDs count]==2)
{
matchRequest.minPlayers = 4;
matchRequest.maxPlayers = 4;
matchRequest.playersToInvite = nil;
[[GKMatchmaker sharedMatchmaker] addPlayersToMatch:match matchRequest:matchRequest completionHandler:^(NSError *error)
{
if (error)
{
NSLog(@"An error occurred during adding a player to match: %@", [error localizedDescription]);}
else if (match != nil)
{NSLog(@"A player has been added to the match");}
}];
}
if ([match.playerIDs count]==3)
{
NSLog(@"Starting Game-589");
// [CCTimer timerWithTarget:self selector:@selector(StartMultiplayerGameTablehost) interval:10];
//[self performSelector:@selector(StartMultiplayerGameTablehost) withObject:nil afterDelay: 1.];
} // end if (match.expectedPlayerCount==0)
else if (boo_gamestarted==false)
{
NSLog(@"player count = %d",[match.playerIDs count]);
NSLog(@"Starting Game-597");
// [self performSelector:@selector(StartMultiplayerGameTablehost) withObject:nil afterDelay:1.];
}
//[CCTimer timerWithTarget:self selector:@selector(StartMultiplayerGameTablehost) interval:10];
// [self performSelector:@selector(StartMultiplayerGameTablehost) withObject:nil afterDelay:3.];
// }
//
else
{
boo_startgame=true;
matchRequest.minPlayers = 3;
matchRequest.maxPlayers = 4;
matchRequest.playersToInvite = nil;
[[GKMatchmaker sharedMatchmaker] addPlayersToMatch:match matchRequest:matchRequest completionHandler:^(NSError *error)
{
if (error)
{
NSLog(@"An error occurrred during adding a player to match: %@", [error localizedDescription]);}
else if (match != nil)
{NSLog(@"A player has been added to the match");}
}];
[self performSelector:@selector(StartMultiplayerGameTablehost) withObject:nil afterDelay:3.];
}
}
}
}
-(void)StartMultiplayerGameTablehost
{
boo_gamestarted=true;
[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer node]];
}
-(void)dealloc
{
[super dealloc];
}
最终结果是一条如下消息:
#0 0x32499468 in ___forwarding___ ()
#1 0x323f0f68 in __forwarding_prep_0___ ()
#2 0x31ffac5e in _Block_object_assign ()
#3 0x39614614 in __copy_helper_block_333 ()
#4 0x31ffa928 in _Block_copy_internal ()
#5 0x34a86694 in _dispatch_Block_copy ()
#6 0x34a8894e in dispatch_group_async$VARIANT$up ()
#7 0x39613c4e in -[GKConnectionInternal connectParticipantsWithConnectionData:withSessionInfo:] ()
#8 0x32c5e21c in -[GKMatch connectToPeersWithDictionaries:version:sessionToken:cdxTicket:] ()
#9 0x32c694b2 in __block_global_17 ()
#10 0x34a8711e in _dispatch_call_block_and_release ()
#11 0x34a864b6 in _dispatch_client_callout ()
#12 0x34a87dca in _dispatch_main_queue_callback_4CF$VARIANT$up ()
#13 0x3246af3a in __CFRunLoopRun ()
#14 0x323ddebc in CFRunLoopRunSpecific ()
#15 0x323ddd48 in CFRunLoopRunInMode ()
#16 0x3a1db2ea in GSEventRunModal ()
#17 0x39c992f8 in UIApplicationMain ()
#18 0x0009f370 in main ()
这些都不是我现在的代码。
答案 0 :(得分:0)
答案最终是我需要发布
matchRequest.minPlayers = 3;
matchRequest.maxPlayers = 4;
在重新定义他们的价值观之前。