Game Development Reference
In-Depth Information
public class Space extends Sprite
{
private static const SCROLL_SPEED:Number = 20;
//PIXELS PER SECOND
private var _previousTime:int = 0;
private var _deltaTime:Number = 0;
public function Space()
{
cacheAsBitmap = true;
}
public function update():void
{
if (_previousTime)
{
_deltaTime = (getTimer() - _previousTime)/1000;
x -= SCROLL_SPEED * _deltaTime;
}
_previousTime = getTimer();
}
}
Since this Sprite is actually the same smaller image tiled three
times (so that it can appears to seamlessly stretch on forever), the
constructor caches the whole object as a bitmap so that the GPU
can render it more quickly. Since we won
'
t be doing anything but
translation ( x and y movements), there
s no need to use cacheAs-
BitmapMatrix just yet. The update method simply detects the
amount of time that has passed since it was last called and moves
itself the appropriate distance based on the constant scrolling
speed of 20 pixels per second. In a more robust application, it
would be wise to store separate values for x and y movements so
as to be able to tile the image in any direction. For now, this will
suffice.
'
Rock.as
The Rock class will handle creation of our floating debris and the
randomization of size and speed, which will give this example a
dynamic feel. Here, we will see the use of cacheAsBitmapMatrix
and object recycling.
public class Rock extends Sprite
{
Search Nedrilad ::




Custom Search