Java Reference
In-Depth Information
• Determine (and set) whether or not the
Layer
is visible using the
isVisible
and
setVisible
methods.
• Set its position (either by offset or absolute positioning relative to the
Graphics
object responsible for painting the object) using the
move
and
setPosition
methods.
The
Layer
itself is interesting, but it's not as interesting as the
TiledLayer
and
Sprite
subclasses, which provide concrete implementations of
Layer
.
You use the
TiledLayer
to present large images consisting of small regular repeating
bitmaps, or tiles. The
TiledLayer
is good for providing game backgrounds as well as inter-
mediate layers. A
TiledLayer
instance divides a large region into
cells
, each of which is
assigned a
tile
, a subimage from within the
TiledLayer
's
Image
instance.
While you use the
TiledLayer
for large static objects, you use the
Sprite
for smaller
animated objects. Like a
TiledLayer
,a
Sprite
takes an image that gets divided up into
smaller images, called
frames
. Unlike a
TiledLayer
,a
Sprite
displays a
single
frame at
its location, letting you choose which frame should be displayed.
Sprite
instances can
transform the frame they're displaying through rotation or mirror flipping, giving you
the ability to specify various appearances of an animated item using only a handful
of frames.
The various layers are tied together using the
LayerManager
class, which maintains
an ordered list of the
Layer
s it must draw. This ordered list provides Z-ordering for the
frames; the first item in the list (at index
0
) is closest to the user. Using the methods of
the
LayerManager
, you can add and remove layers from this list, as well as repaint the
individual
Layer
s.
Managing Layers
The
LayerManager
's primary responsibility is to help you keep the
Layer
s in your game
organized. To do this, the
LayerManager
encapsulates a list of
Layer
s and provides a view
window that defines the size of the visible region of the
Layer
s and the visible region's
position relative to the
LayerManager
's coordinate system. By panning the view window,
you can pan the display across a collection of layers, creating a viewable window that
scrolls in any direction over the game world. You do this using the
setViewWindow
method,
passing the bounds (e.g., left, top, width, height) of the view window. For example, to
scroll right, simply move the view window to the right (increment the
x
coordinate when
invoking
setViewWindow
). Often, you want to coordinate this behavior with the movement
of the user character's
Sprite
, so that the game world visible corresponds to the immedi-
ate surroundings of the user.
The majority of the
LayerManager
methods correspond to actions you can take on the
LayerManager
's list of
Layer
s, as follows: