Game Development Reference
In-Depth Information
The class starts by defining the constant value for how often to create
new rocks. There is also a Dictionary object for keeping track of all the
rocks in use. Inside the constructor, an enterFrame listener is attached,
which will drive all of the updates. It also creates a new instance of the
Space Sprite and sets up the timer for generating new Rocks.
private function onRockTimer(e:TimerEvent):void
var rock:Rock = Rock.getRock();
rock.x = -rock.width * 2;
rock.y = Math.random() * loaderInfo.height;
_rockList[rock] = rock;
Whenever the timer for creating new rocks is called, you can see
that the static Rock.getRock method is used. This will allow us to
use either new or recycled instances of the class as it sees fit and
does not complicate our implementation. Once a
rock is
retrieved, it is placed just offscreen at a random y value and added
to the Dictionary object.
private function onEnterFrame(e:Event):void
for each (var rock:Rock in _rockList)
if (rock.x > loaderInfo.width + rock.width)
delete _rockList[rock];
if (_space.x < = -_space.width/3)
_space.x = _space.x + (_space.width/3);
On every frame, the class iterates over all of the rocks in the
Dictionary object, calling their update methods. Once a rock is out-
of-bounds (has left the other side of the screen), it is removed
updated, repositioning itself onceitgetstothepointwhereitcan
loop back to create a seamless texture.
Search Nedrilad ::

Custom Search