Game Development Reference
In-Depth Information
type. In this, a player allotment packet type named
KNetworkPack-
etCodePlayerAllotment
is passed, hence the data retrieved is
gameU-
niqueId
. As discussed above, in
didMoveToView
, we assigned a random
number to a variable named
gameUniqueIdForPlayerAllocation
for
both the devices. Hence for both devices, different numbers are generated &
while sending the allotment packet from both devices, this is passed as the data
(the allotment packet to be sent would be discussed in next point). Finally to de-
cide which is first and second player, the local value of
gameU-
niqueIdForPlayerAllocation
would be compared with the value sent in
the packet, on this comparison one would be assigned as the first player and other
as the second player, informing the users by changing appropriate text of
gameInfoLabel
as shown in the delegate method.
5. Remove the following written line from the public method
startGame
of
GameScene
as now, the
gameInfoLabel
will be set according to the packet
received.
self.gameInfoLabel.text = kConnectedDevicesText;
6. All these earlier processes start when the user clicks on the done button. This but-
ton is an indication that the players have been connected and a delegate method
didChangeState
of
MCSession
will be called with a
MCSessionState
called
MCSessionStateConnected
, and as a checking protocol is already
in-built in this method of connected state, add the following code in the
if
state-
ment:
- (void)session:(MCSession *)session peer:(MCPeerID
*)peerID didChangeState:(MCSessionState)state {
// A peer has changed state - it's now either
connecting, connected, or disconnected.
if (state == MCSessionStateConnected)
{
NSLog(@"state == MCSessionStateConnected");
// Remember the current peer
self.gamePeerID = peerID;
// Make sure we have a reference to the game
session and it is set up
self.gameSession = session;