End Match if Home Button is Pressed
How wou开发者_运维问答ld I end a match/disconnect the opposing player and/or remove the match maker standard interface (GameKit), if the Home button on the device is pressed? I also have a "Main Menu" button on screen and if that is pressed, I would like to end the match/disconnect the opposing player. I'm using GameKit to find and start matches.
I am new to multiplayer so any help is appreciated.
-(void) match:(GKMatch*)match player:(NSString*)playerID didChangeState:(GKPlayerConnectionState)state
{
switch (state)
{
case GKPlayerStateConnected:
[delegate onPlayerConnected:playerID];
break;
case GKPlayerStateDisconnected:
CCLOG(@"Disconnected");
[delegate onPlayerDisconnected:playerID];
break;
}
if (matchStarted == NO && match.expectedPlayerCount == 0)
{
matchStarted = YES;
[delegate onStartMatch];
}
}
-(void) disconnectCurrentMatch
{
CCLOG(@"Quit Match");
[currentMatch disconnect];
currentMatch.delegate = nil;
[currentMatch release];
currentMatch = nil;
}
That is all I am using to disconnect players. Then when the player presses the "Main Menu" button this is the code.
-(void)Menu: (id) sender
{
didQuit = YES;
if (isSingle == YES)
{
GameKitHelper* gkHelper = [GameKitHelper sharedGameKitHelper];
[gkHelper disconnectCurrentMatch];
gkHelper.delegate = nil;
}
[[CCDirector sharedDirector] replaceScene: [MainMenu scene]];
CCLOG(@"Return Main Menu");
}
It replaces the scene to the Main Menu, but never changes the state to disconnected.
Jon,
When the home button is pressed, your application delegate will receive the -(void)applicationDidEnterBackground:(UIApplication*)application
callback.
You have roughly 5 seconds (before Springboard kills your app) to do whatever you need to do to clean up your app (unless you need more time, which you can request from iOS, but aren't guaranteed to receive).
How do you "clean up"? As you mentioned - your match has 2 players, so either player disconnecting or quitting has the same effect on the game - ending it (assuming that a player cannot continue to play on by him/herself).
Your leaving player will send a -disconnect
message to GameKit.
The player left behind's program needs to have an object that conforms to the GKMatchDelegate protocol and receives callbacks as the delegate of GKMatch
.
When an opposing player disconnects, GKMatch will call to the delegate with:
- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state`
So, now your player that has been left behind's code will have also been notified. In that method, check if it's disconnection - and if so, follow your own logic for what you want to happen when a player is "hung up on".
精彩评论