Game Development Reference
In-Depth Information
How to do it
Now, on tapping the screen, a MCSession with a service type has been instantiated; we can
use this session and service type to present a
MCBrowserViewController
and estab-
lish connection between players (that is, devices).
MCBrowserViewController
is
fully equipped and designed for connecting multiple players for a session provided in the
Multipeer Connectivity framework. These are the steps involved:
1. First of all, create a protocol of
GameScene
as
GameSceneDelegate
and its
delegate object in
GameScene
, which will be set as
GameViewController
so
that it uses its delegate method when the user touches the screen.
GameViewCon-
troller
can be informed to present
MCBrowserViewController
. Declare
the protocol code and
GameSceneDelegate
object, as follows:
@protocol GameSceneDelegate <NSObject>
-
(void)showMCBrowserControllerForSession:(MCSession*)sessionserviceType:(NSString*)serviceType;
@end
@property (nonatomic, weak) id<GameSceneDelegate>
gameSceneDelegate;
2. When the user touches the screen that has the
gameState
as
kGameStatePlayerToConnect
, where we are calling the method,
instan-
tiateMCSession
, which also informs
gameSceneDelegate
to show
MCBrowserViewController
by passing
gameSession
that was created
and the
serviceType
property:
if (self.gameSceneDelegate && [self.gameSceneDelegate
respondsToSelector:@selector(showMCBrowserControllerForSession:serviceType:)])
{
[self.gameSceneDelegate
showMCBrowserControllerForSession:self.gameSession
serviceType:self.serviceType];
}
3. The delegate method has to be called by
GameViewController
, and on the
same controller,
MCBrowserViewController
has to be presented, which will