Java Reference
In-Depth Information
this case, we want the hero to walk downwards, so we choose to use
only frames 6, 7 and 8:
guy = new Sprite(guyImage,SPRITE_WIDTH,SPRITE_HEIGHT);
guy.setFrameSequence(new int[]
{
6,7,8
}
);
We then create the background for the game scene, by constructing a
new
TiledLayer
from the source image (see Figure 2.9), and use the
fillLayer()
method and the cells array to create a terrain over which
our hero will walk.
Figure 2.9
Background image for the
LayerManagerDemo
MIDlet
We create a
LayerManager
to manage both the background
Tiled-
Layer
and the
Sprite
(both inherit from
Layer
). We append the
Sprite
then the background layer, to ensure that the hero is shown
over the background and not the other way around, since the hero has a
smaller z-index. This is how it's done in code:
// creating LayerManager
manager = new LayerManager();
manager.append(guy);
manager.append(background);
manager.setViewWindow(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
We also set the view window to match the size of the screen so we
can see it in its entirety, and not just a portion of it. Try playing with the
values for the view window and you get partial views of the background