Game Development Reference
In-Depth Information
The constructor for the engine accepts two classes that it will be
responsible for instantiating. The first is the class used for the ball;
in theory, this could be any class that implements IBall, but in
practice it will be some kind of DisplayObject.Assuch,wecastit
and store the second reference to prevent casting it again when we
need it later. The level class is also instantiated, validated (through
a method we
s dis-
play list. The ball is then added to the level and has its position
reset.
This class has three public methods that can be called from
other classes. We
'
ll look at shortly), and then added to the engine
'
'
ll look at these next.
public function startLevel(callback:Function):void
{
_levelEndCallback = callback;
_accelerometer = new Accelerometer();
_accelerometer.setRequestedUpdateInterval(100);
_accelerometer.addEventListener(AccelerometerEvent.
UPDATE, onAccelerometerUpdate, false, 0, true);
_previousTime = getTimer();
addEventListener(Event.ENTER_FRAME, onEnterFrame,
false, 0, true);
}
public function endLevel(callback:Boolean = true):void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
_accelerometer.removeEventListener(AccelerometerEvent.
UPDATE, onAccelerometerUpdate)
if (callback && _levelEndCallback != null) _levelEndCallback();
}
public function dispose():void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
removeChild(_currentLevel);
_currentLevel = null;
_ball = null;
_ballDO = null;
_accelerometer = null;
}
In startLevel , the engine creates an accelerometer instance,
sets it to update every tenth of a second, and starts the game
loop. It also accepts a function that will later be called when the
level ends. As you might expect, endLevel undoes pretty much
everything startLevel does, with the option to trigger the callback
function if it exists. Finally, dispose can be called when the engine
Search Nedrilad ::




Custom Search