Java Reference
In-Depth Information
greater z-values and indices. Alternatively, we can add a layer at a specific
index using the method:
public void insert(Layer l, int index);
To remove a layer from the list we use the method:
public void remove(Layer l);
We position a layer in the
LayerManager
's coordinate system using
the
setPosition()
method. The contents of
LayerManager
are not
rendered in their entirety; instead, a view window is rendered using the
paint()
method of the
LayerManager
:
public void paint(Graphics g, int x, int y);
The
x
and
y
arguments are used to position the view window on
the displayable object (
Canvas
or
GameCanvas
) upon which the
Lay-
erManager
is rendered. The size of the view window is set using this
method:
public void setViewWindow(int x, int y, int width, int height);
The
x
and
y
values determine the position of the top left corner of
the rectangular view window in the coordinate system of the
Layer-
Manager
. The
width
and
height
arguments determine the width and
height of the view window and are usually set to a size appropriate for the
device's screen. By varying the x and y coordinates we can pan through
the contents of the
LayerManager
.
LayerManagerDemo
Example
Our
LayerManagerDemo
example summarizes all Game API concepts
seen so far. It illustrates the use of a
Sprite
(for animating the hero),
a
TiledLayer
(for background construction), a
LayerManager
(for
scrolling the background), and a
GameCanvas
(for drawing it all). The
source code is too large to be included in this topic, so it is available for
download from the website. We highlight here some parts that show how
to use the Game API components.
In the constructor, we load the image resources used by our
Tiled-
Layer
and the
Sprite
(see Figures 2.6 and 2.8, respectively). We
create our game hero, using the
Sprite
constructor, then set the frame
sequence, which allows us to loop only to the frames that interest us. In