Game Development Reference
In-Depth Information
Handling controller notifications
Another thing that we would want to handle is new controllers. We need to do
something when a controller connects or disconnects.
We can create a complex interface for that, but for our project, simple alerts will do.
The easy part is that when the controller disconnects, we won't get any new input,
so we don't need to do anything; we just show an alert and are done with it:
- (void)gameControllerDidDisconnect:(NSNotification *)notification {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
message:@"Game
controller has disconnected."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
}
We create the
alertView
object and show it. The user cancels it and goes on with
their game with touch controls.
But if we connect the controller, we need to ask the user if they actually want to
use it in the game. In order to do that, we need to set our scene as a delegate to
UIAlertView
and change the
@interface
line in
ERGMyScene.h
to the following:
@interface ERGMyScene : SKScene <SKPhysicsContactDelegate,
UIAlertViewDelegate>
This means that we conform to
UIAlertViewDelegate
and that we will implement
certain methods to be executed after the user taps a button in the alert view.
When a new controller connects, we will get a notification and the following method
will get called, which should be added to the
ERGMyScene.m
ile:
- (void)gameControllerDidConnect:(NSNotification *)notification {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
message:@"Game
controller connected. Do you want to use it?"
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes",
nil];
[alert show];
}