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();
addChild(rock);
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.
new
private function onEnterFrame(e:Event):void
{
//UPDATE ROCKS
for each (var rock:Rock in _rockList)
{
rock.update();
if (rock.x > loaderInfo.width + rock.width)
{
removeChild(rock);
delete _rockList[rock];
rock.reset();
}
}
//UPDATE SPACE
_space.update();
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
fromthelistofactiverocksandreset.TheSpaceinstanceisalso
updated, repositioning itself onceitgetstothepointwhereitcan
loop back to create a seamless texture.
Search Nedrilad ::




Custom Search