Game Development Reference
In-Depth Information
[[NSNotificationCenter defaultCenter] addObserver:self selector:@
selector(gameControllerDidDisconnect:) name:GCControllerDidDisconnectN
otification object:nil];
[self configureConnectedGameControllers];
[GCController
startWirelessControllerDiscoveryWithCompletionHandler:^{
// we don't use any code here since when new controllers are
found we will get notifications
}];
}
The first thing that we do is subscribe for notifications related to the game controller.
They are sent when a new controller is connected or disconnected from the device.
Next, we call another method to configure the controller.
Also, notice that if you turn off Bluetooth on your device, the game will crash on the
configureGameControllers method. The same thing will happen if you test on a
simulator. Adding error-checking unnecessarily complicates the code in our example,
so comment out [self configureGameControllers]; in the initWithSize:
method if you don't intend to use it on a Bluetooth-enabled device.
We also start searching for wireless controllers. We don't need any code inside the
completion handler as we will be getting notifications when controllers connect or
disconnect from the device. Let's get to the next method:
- (void)configureConnectedGameControllers {
for (GCController *controller in [GCController controllers]) {
[self setupController:controller forPlayer:self.player];
}
}
This is a simple for loop, where we will go through all the available controllers
and run a common setup code for each of them. If you are making a more complex
or multiplayer game, you might want to differentiate the controllers here. There is
a handy playerIndex property on the controller that may prove useful for you,
but we don't use it here.
 
Search WWH ::




Custom Search