Game Development Reference
In-Depth Information
The important concept here is that we have defined a name for our accelerometer object,
accelerometer , and we have set its updateInterval property to kPollingRate . This
constant was defined in tiltViewController.h as (1.0f/60.0f) , which corresponds to 60
Hz. In other words, this tells the operating system to update our program's accelerometer
object 60 times a second. Also in tiltViewController.m , we write what happens when the
accelerometer object gets updated via the accelerometer's didAccelerate: function as
follows:
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration{
[(SpriteView *)self.view setAcceleration:acceleration];
[(SpriteView *)self.view draw];
}
This function is called every time the acceleration object is updated and does two things.
First, it takes the acceleration data from the accelerometer and passes it to the Sprite
View class, which we'll talk about in a second. Then it tells the SpriteView to go ahead
and redraw itself.
The SpriteView class is where the action happens and consists of a header file, Sprite
View.h , where we define the following global variables:
UIImage *sprite
A pointer to the image that will be used to represent our sprite on the screen.
currentPos
The position on the screen where we want the sprite to be drawn.
prevPos
The previous position of the sprite on the screen. We will use this to tell the draw
function what parts of the screen need to be redrawn.
UIAcceleration *acceleration
A special Objective-C data type to hold data from the accelerometer.
CGFloat xVelocity and CGFloat yVelocity
Float variables to hold the current velocity in the x-direction and y-direction, re‐
spectively.
CGFloat convertX and CGFloat convertY
Float variables to hold the ratios for converting our physics engine's results in meters
to pixels based on an assumed world size.
Additionally, we've defined the following global constants:
g
Near earth gravity value, set at 9.8 m/s 2 . This will convert the accelerometer's values
from g to m/s 2 for use in calculating velocity. This can also be tuned to represent an
Search Nedrilad ::




Custom Search