Game Development Reference
In-Depth Information
Listing 8-34. AccelerometerController.h
@interface AccelerometerController : GameController<UIAccelerometerDelegate>{
Viper* viper;
}
@end
In Listing 8-34, we see the header for the class AccelerometerController . The things to note here
are that the class AccelerometerController conforms to the protocol UIAccelerometerDelegate , and
that it has a reference to the Viper that will be moving around the screen. Conforming the protocol
allows AccelerometerController to receive data from the accelerometer in real time, as we will see
in a bit. Let's first look at how I set up the class AccelerometerController so it receives data, as
AccelerometerController.m (doSetup)
if ([super doSetup]){
[self setGameAreaSize:CGSizeMake(320, 480)];
viper=[Viper viper:self];
[self addActor:viper];
UIAccelerometer* theAccelerometer=[UIAccelerometer sharedAccelerometer];
theAccelerometer.updateInterval=1 / 50.0;
theAccelerometer.delegate=self;
return YES;
}
return NO;
}
In Listing 8-35, we do the usual setup, specifying the game size area and adding the Viper that we
will be moving around with accelerometer data. To start receiving accelerometer data, we have to get
access to the singleton UIAccelerometer associated with the running application. To do this, we use
the static task sharedAccelermoter of the class UIAccelermoter , and then we simply set self as the
delegate. Another thing to note is that we have to specify the frequency at which we get data. In this
case, we are requesting data 50 times a second, which is plenty fast for real-time input. The data is
actually received by a call to accelerometer:didAccelerate :, as shown in Listing 8-36.
Listing 8-36. AccelerometerController.m (accelerometer:didAccelerate:)
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
CGSize size=[self gameAreaSize];
UIAccelerationValue x, y, z;
x = acceleration.x;
y = acceleration.y;
 
Search WWH ::




Custom Search