Game Development Reference
In-Depth Information
is being discarded to help with quicker garbage collection. Next,
we
ll look at the two event handlers for the Accelerometer and
game loop.
'
private function onAccelerometerUpdate(e:AccelerometerEvent):void
{
_ax = (_ax + e.accelerationX)/2;
_ay = (_ay - e.accelerationY)/2;
}
private function onEnterFrame(e:Event):void
{
var currentTime:Number = getTimer();
var deltaTime:Number = (currentTime - _previousTime)/1000;
_previousTime = currentTime;
moveBall(deltaTime);
checkCollisions();
checkHazards();
checkWin();
}
Whenever an Accelerometer update occurs, the x and y values
are pulled from the resulting event and averaged with the values
stored in the engine. This will help with the sometimes erratic
jumps and spikes in the numbers returned by the device. You
may also notice that we add the value to the x acceleration but
subtract it from the y .ThisisbecauseFlash
scoordinatesystem
is inverted from a typical Cartesian coordinate
plane. In traditional geometry, x values increase
as you move from left to right and y values
increase as you move up ,asshownin Fig. 15.6 .
In Flash, y values increase as you move down ,
so the values for acceleration along that axis
must also be inverted.
The onEnterFrame game loop basically acts as
the catalyst for several other methods that are
called. We
'
y
(2, 3)
3
2
( 3, 1)
1
ll look at each of these in order they
appear here, but here
'
(0, 0)
s a quick summary of what
takes place every frame:
1.
'
x
3
2
1
1
2
3
1
The ball is moved based on how much time
has passed since the last frame.
2
2.
The engine detects all collisions with walls and
( 1.5, 2.5)
3
3.
The engine detects any collisions with hazards
in the level.
4.
The engine checks to see if the player has
reached their destination.
Figure 15.6
The Cartesian coordinate system.