Java Reference
In-Depth Information
Listing 8-7.
Creating a Sprite Instance
private final int tileWidth = 16, tileHeight = 16;
Image image = Image.createImage(imageName);
Sprite butterfly = new Sprite(image, tileWidth, tileHeight);
Every
Sprite
has a frame sequence that defines an ordered list of frames to be dis-
played. By default, this is simply the order of the list of available frames. You can explicitly
set this sequence using the method
setFrameSequence
, which takes an array of frame
indexes, as shown in Listing 8-8.
Listing 8-8.
Setting the Frame Sequence
private static final int[] flightSequence = {
0, 1, 2, 2, 1, 0};
butterfly.setFrameSequence(flightSequence);
At any time, you can change which frame the
Sprite
will draw using any one of the
methods
setFrame
,
prevFrame
, or
nextFrame
.
■
Tip
Remember that
setFrame
,
prevFrame
, and
nextFrame
deal with indexes into the
frame sequence
,
not the image of frames!
Each
Sprite
instance has a
reference pixel
that indicates the location from which
the
Sprite
will draw its frame. By default, this is simply the upper left-hand corner of
the frame, but you can change this position using the
defineReferencePixel
, passing the
x
and
y
coordinates of the new reference pixel. Reference pixels are especially handy
when you consider that the
Sprite
class can apply various visual transformations to the
frames that make up the
Sprite
. These transforms include rotations in multiples of 90°
and mirroring around the vertical axis of each of these rotations. When the
Sprite
class
applies a transformation, the
Sprite
is automatically repositioned so that the reference
pixel appears stationary; for example, rotation occurs about the reference pixel. You
can apply a transformation to a
Sprite
using the
setTransform
method, passing one of
the constants shown in Table 8-2.