Game Development Reference
In-Depth Information
{
[self.gameSession sendData:packet
toPeers:[NSArray
arrayWithObject:peerId]withMode:MCSessionSendDataUnreliableerror:&error];
}
if (error)
{
NSLog(@"Error:%@",[error description]);
}
}
}
Here networkPacket is created with a header and data. A variable
pIntData is declared, which is the header containing the NetworkPack-
etCode and a gamePacketNumber so that a unique number is assigned to a
packet to serialize the network packets to be used to sync or update the game
properly. Once the packet is created, a method called sendData of MCSes-
sion is called with the packet to be sent, peerID to which the packet has to be
sent, the mode, which can be MCSessionSendDataUnreliable or
MCSessionSendDataReliable and error to check whether an error has
occurred while sending the packet.
This method will be reused everywhere in the game to send packets to peers of
the same game.
3. Generate a random number and store it in the variable gameU-
niqueIdForPlayerAllocation declared above, which will help to decide
which will be the first and second player. Add this line in the didMoveToView
method of GameScene .
gameUniqueIdForPlayerAllocation = arc4random();
4. Add the following code to receiving data delegate method of MCSession for
handling of received packets based on their NetworkPacketCode as shown in
the following code:
- (void)session:(MCSession *)session
didReceiveData:(NSData *)data fromPeer:(MCPeerID
*)peerID {
// Data has been received from a peer.
Search WWH ::




Custom Search