This is the collaborative drawing based on turns using Game Center network:
http://www.youtube.com/watch?v=jJBcdCI3vFM
These are the simple steps.
- Create a game in itunesconnect.apple.com, set GameCenter Activated.
- Create a test account for simulating two players (one in your iPad and the other in the simulator)
- Export your Codea project and embed the ObjC classes inside the game
PLAY.
Let me explain how Game Center turn based plays works.
You have to authenticate and all that stuff that @Reefwing explained in his tut some time ago.
When you have a valid login then you have to call for find a match with a number of participants with a function like this :
- (void)findMatchWithMinPlayers:(int)minPlayers maxPlayers:(int)maxPlayers { if (!hasGameCenter) return; self.codeaViewController.paused = YES; GKMatchRequest *request = [[GKMatchRequest alloc] init]; request.minPlayers = minPlayers; request.maxPlayers = maxPlayers; GKTurnBasedMatchmakerViewController *mmvc = [[GKTurnBasedMatchmakerViewController alloc] initWithMatchRequest:request]; mmvc.turnBasedMatchmakerDelegate = self; mmvc.showExistingMatches = YES; [self.codeaViewController presentViewController: mmvc animated:YES completion:nil]; } ``` I'm using only 2 players for the collaborative drawing game but it could be done with more, imagine 12+ players drawing over the same image xD. This will show a window with the GameCenter Match Finder, you can invite a friend or set it to Auto-Match...when you choose, this is the event called:-(void)turnBasedMatchmakerViewController: (GKTurnBasedMatchmakerViewController *) viewController didFindMatch:(GKTurnBasedMatch *) match { [self.codeaViewController dismissModalViewControllerAnimated:YES]; self.currentMatch = match; GKTurnBasedParticipant *firstParticipant = [match.participants objectAtIndex:0]; if (firstParticipant.lastTurnDate == NULL) { // It's a new game! [matchDelegate enterNewGame:match]; } else { if ([match.currentParticipant.playerID isEqualToString:[GKLocalPlayer localPlayer].playerID]) { // It's your turn! [matchDelegate takeTurn:match]; } else { // It's not your turn, just display the game state. [matchDelegate layoutMatch:match]; } } } ``` There is variable called matchDelegate, it handles all the action of a match from @Reefwing GC Addon class. This is the class prototype:@interface MatchDelegate : NSObject { GKTurnBasedMatch *currentMatch; } @property (retain) NSMutableDictionary *playersDict; @property (retain) GKTurnBasedMatch *currentMatch; - (void)enterNewGame:(GKTurnBasedMatch *)match; - (void)layoutMatch:(GKTurnBasedMatch *)match; - (void)takeTurn:(GKTurnBasedMatch *)match; - (void)recieveEndGame:(GKTurnBasedMatch *)match; - (void)sendNotice:(NSString *)notice forMatch:(GKTurnBasedMatch *)match; - (void)sendTurn; - (int) getCurrentMatchStatus; - (void)leaveMatch; @end ``` You have to save the *match parameter of the handles coming from the GameCenter network into that MatchDelegate.currentMatch to know everything about the current match, like the current turn, the players, etc. Register lua calls to read that info from the matches inside the Codea project. I'm making a loop reading the match status, and change the game status when the match status change, so you can know what you have to do anytime. Once you have created a game, the first turn starts, you can cancel the turn or finish it, in this case ,you have to use the sendTurn function, you can use a message (NSString) inside match.message and a NSData inside match.matchData, there is a limit for this data, 65536 bytes, that's why the images sent in the video looks ugly, I dont know how to fix it right now without using paths, but this info (vectors,colors,brushes,sizes,etc) could be much more larger that the resized image. You can know info about the match, so, when it is not your turn you can layout the state of the game, getting the match.matchData and match.message from the currentMatch inside the MatchDelegate variable of your GameCenterAddon class. When it is your turn, you use this info again to rebuild the game state and continue with your turn, then send it again until you set the turn as the last turn. I'm not using this function because there is no goal in this game and the info can't grow, you are just sending the same image modified. To send the turn you can do this in the MatchDelegate:NSUInteger currentIndex = [currentMatch.participants indexOfObject:currentMatch.currentParticipant]; GKTurnBasedParticipant *nextParticipant; nextParticipant = [currentMatch.participants objectAtIndex: ((currentIndex + 1) % [currentMatch.participants count ])]; [currentMatch endTurnWithNextParticipant:nextParticipant matchData:data completionHandler:^(NSError *error) { if (error) { NSLog(@"%@", error); } } ]; NSLog(@"Sending Turn, %@, %@", data, nextParticipant); //this is done is background currentMatch = nil; ``` You can see the game is saving a very low quality image, that's because of the limitations of the data size, we'll need to figure out how to compress the image data in a better way without using external services... You have the full tut here: http://www.raywenderlich.com/5480/beginning-turn-based-gaming-with-ios-5-part-1