Game Development Reference
In-Depth Information
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
In Listing 8-32, we see three tasks. The first task, canBecomeFirstResponder , simply informs the
application that the view property of this UIViewController is eligible to be first responder, and that
this UIViewController will handle its events. The next two tasks are part of a UIViewController's
viewDidAppear : is called and this UIViewController's view property is displayed,
becomeFirstResponder . Or, when this UIViewsController
UIViewController to be first responder,
resignFirstResponder . These three tasks simply make this class eligible to receive
motionBegan:withEvent :
motionEnded:withEvent :. We only need to implement one of these in our example: the
motionBegan:withEvent : is shown in Listing 8-33.
ShakeController.m (motionBegan:withEvent:)
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake)
{
for (Asteroid* asteroid in [self actorsOfType:[Asteroid class]]){
[asteroid doHit:self];
}
}
}
In Listing 8-33, we see the task motionBegan:withEvent :, where we first check that the motion sub-
type is a UIEventSubtypeMotionShake . If it is, we simply iterate over all of the asteroids in the scene
and call doHit : on them. The task doHit : causes each asteroid to break apart into a number of
smaller asteroids. The details of doHit : are discussed in Chapter 7.
As we can see, responding to shake events is pretty simple—very much like responding to touch
events. I find it a little weird that shake events are not implemented as a gesture. I suspect the
reason is that shake events have been around before the many different gesture recognizers made it
into the SDK.
Responding to Accelerometer Data
As a developer, being able to know the orientation of the device is really cool to me. It allows things
like augmented reality to be possible, as well as a bunch of other cool things. The last example
in this chapter will use the accelerometer to manipulate actors in our scene. I can't promise it will
 
Search WWH ::




Custom Search