Java Reference
In-Depth Information
This image is 192×48 pixels. If it is created with a 48×48-pixel frame size, there are four
frames. The default frame sequence is { 0, 1, 2, 3 } . Note that frame indices are numbered
starting at zero, while tile indices (in the TiledLayer class) are numbered starting at one.
In Figure 14-5, the first three frames represent Dr. Quatsch running, while the fourth is a
frame that shows him standing still. The following method changes the current frame sequence:
public void setFrameSequence(int[] sequence)
For example, the following code shows how you could create a new Sprite and set its
frame sequence to include only the running frames:
int[] runningSequence = { 0, 1, 2 };
Image quatschImage = Image.createImage("/quatsch.png");
Sprite quatsch = new Sprite(quatschImage, 48, 48);
quatsch.setFrameSequence(runningSequence);
Sprite provides several methods for navigating through the frame sequence. The anima-
tion doesn't happen automatically; your application needs to tell the Sprite when it's time to
move to the next frame in the sequence. Usually this is accomplished in a separate thread,
most likely as part of the animation thread. To move forward and backward in the sequence, use
nextFrame() and prevFrame() . These methods do what you'd expect at the ends of the sequence,
wrapping around to the next value. For example, using the frame sequence of { 0, 1, 2 } ,
if the Sprite 's current frame is 2 and you call nextFrame() , the current frame will be set to 0 .
You can jump directly to a particular frame using this method:
public void setFrame(int sequenceIndex)
Note that this method accepts a sequence index. If the Sprite 's frame sequence is
{ 2, 3, 1, 9 } , then calling setFrame(1) would result in the Sprite 's current frame being
set to 3 .
Nothing happens visually when you adjust the Sprite 's current frame. Changes will only
be visible the next time the Sprite is rendered using its paint() method. Typically, this will be
at the end of your animation loop if you are using GameCanvas .
To find out the current frame sequence index, call getFrame() . Don't get confused here;
the method does not return a frame index, but the current index in the current frame sequence.
Interestingly, there is no getFrameSequence() method, so if you haven't saved the current frame
sequence, there's no way to find out the current frame index. You can, however, retrieve the
number of elements in the current frame sequence using getFrameSequenceLength() .
Transforming Sprites
You may have noticed that the frames shown in Figure 14-5 only show Dr. Quatsch facing left.
What if he's going to run to the right? Sprite includes support for transformations so that you
can use the API to generate additional frames that are simple transformations of existing frames.
This approach reduces the total number of stored images, effectively reducing resource (disk
space, memory, etc.) usage. The following method applies a transformation to a Sprite :
public void setTransform(int transform)
The transform argument can be any of the constant values defined in the Sprite class:
Search WWH ::




Custom Search