Game Development Reference
In-Depth Information
MCPeerID* peerID = [[MCPeerID alloc]
initWithDisplayName:device.name];
self.gameSession = [[MCSession alloc]
initWithPeer:peerID];
self.gameSession.delegate = self;
self.serviceType = @"TankFight"; // should be
unique
self.advertiser = [[MCAdvertiserAssistant alloc]
initWithServiceType:self.serviceTypediscoveryInfo:nil
session:self.gameSession];
[self.advertiser start];
}
}
Here, an object of
MCSession
is created using
peerID
with the display name
as the device name and the
delegate
property of this object is set to the
GameScene
object to get the
delegate
methods implemented in this class.
For invitations to the user and handling all user responses an object of
MCAd-
vertiserAssistant
is created using the
gameSession
and a
ser-
viceType
, which should be unique.
In the fist line of the code snippet we have used the
pragma
marks. Using
pragma
marks we can make our code much more readable and also provide lo-
gical grouping to our methods. It is a good programming practice to follow.
9. Implement all delegates
MCSessionDelegate
with the
pragma
mark as
shown below:
#pragma mark - MCSessionDelegate Methods
- (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");
}
else if (state == MCSessionStateConnecting)