Managing Layers Using the LayerManager
Recall that both the Sprite and TiledLayer classes 
extend the Layer class.
 
 
A D V E R T I S E M E N T 
A game may contain at least one 
TiledLayer and several Sprite classes. With so many layers 
to control, the LayerManager class comes in handy. This class 
provides methods to add, remove, or insert layers from a game, and also provides 
a single method to paint all of these layers to the underlying Graphics 
object. This means that you don't need to individually call the paint() 
method of each of the layers of a game.
An instance of LayerManager is created using its no-args 
constructor. Layers are then added, removed, or inserted into it by using the 
methods append(Layer layer), remove(Layer layer), and
insert(Layer l, int index), respectively. The order in which layers 
are added is important, because this order determines which layer is painted 
first, as this becomes the z-order index. The layer at index 0 is painted on top 
of all the other layers, and hence, is closest to the user, and so on. 
In our game, the start() method now needs to be modified, as 
shown here: 
// creates the layermanager
manager = new LayerManager();
// and adds layers to it
manager.append(coupleSprite);
// creates the game background
createBackground();
manager.append(background);  
As you can see, the coupleSprite layer will be closest to the 
user and the background layer will be farthest back, based on their 
indices. The buildGameScreen() method now does not need to paint 
the background (as the LayerManager will paint the background now), 
and therefore the background.paint(g) line needs to be removed from 
this method. Finally, in the previous section, you used the coupleSprite 
to paint it on the screen instead of the coupleImage. Now even that 
is not required, as the LayerManager will do this for you. Remove
coupleSprite.paint(g) from the updateGameScreen() 
method and replace it with manager.paint(g, 0, 0). As you can see, 
all calls to individual layers' paint() methods have been replaced 
with a single call to the LayerManager's paint() 
method. The last two parameters represent the location at which the manager 
should paint. Since the background and carSprite are 
responsible for their own positioning, you can leave these parameters as is 
(that is, paint from the device origin). 
Listing 3 shows the revised updateGameScreen(). The lines that 
are to be removed are retained as comments to make it easy to locate the 
changes. 
private void updateGameScreen(Graphics g) {
  // the next two lines clear the background
  g.setColor(0xffffff);
  g.fillRect(0, 0, getWidth(), getHeight());
  // creates the game borders
  buildGameScreen(g);
  // draws the couple image according to current
  // desired positions
  /*g.drawImage(
    coupleImg, coupleX,
    coupleY, Graphics.HCENTER | Graphics.BOTTOM);*/
  // animates the sprite
  coupleSprite.nextFrame();
  // moves the sprite based on its reference pixel
  coupleSprite.setRefPixelPosition(coupleX, coupleY);
  // paints it on the buffer
  // coupleSprite.paint(g);
  // the manager paints all the layers
  manager.paint(g, 0, 0);
  // this call paints off screen buffer to screen
  flushGraphics();
} 
Listing 3. Updated updateGameScreen() method 
 |