Game Development Reference
// The rest of the update code remained unchanged.
The update method processing is split into handling the gameover and the rest of the
code that runs while the game is commencing. If the game is over, you simply check
whether any touch ended, or the spacebar or Return key is being held down, before re-
setting the game and starting over.
While the game is running, the player's velocity is updated either based on the in-
put.acceleration.smoothedX value or based on a constant keyAccelera-
tion value when either the left or right arrow key is held down. The acceler-
atePlayerWithX method is shown in Listing 16-6 (coming up soon), which con-
tains the code previously in the accelerometer:didAccelerate method.
KKInput provides you with built-in high-pass and low-pass filters via properties of
the KKAcceleration class exposed by the input.acceleration property. You
can access the raw acceleration, the smoothed (low-pass filtered), and the instantaneous
(high-pass filtered) values. In most games, you want to use the smoothed values, which
provide steady acceleration and cancel out sudden, short-lived movements. Instantan-
eous acceleration values are useful whenever you want to react to sudden acceleration
movements, such as shaking or quickly flipping the device.
You'll notice that the input code doesn't use conditional compiling via #ifdef . If you
run this code on Mac, the anyTouchEnded method is guaranteed to return NO all the
time. Likewise, when running on iOS, the isKeyDown method always return NO , be-
cause there's no keyboard available. And the input.acceleration values are all
0 on Mac OS.
If it seems wasteful to you to test for keyboard events on iOS and touch events on Mac
OS, please keep in mind that the additional overhead is minimal while the benefit of al-
ways compiling all your code guarantees that it continues to work for both platforms. If
the platform-specific code is extensive, you can always branch it using the Kobold2D