Java Reference
In-Depth Information
You pass a regular tile index to the method, which is the initial tile that should be used for
the animated tile. The method returns a special animated tile index. (There's no magic here;
it's just a negative number.)
To assign an animated tile to a cell, pass the return value from
createAnimatedTile()
to
setCell()
. When you want to change the contents of an animated tile, use the following method:
public void setAnimatedTile(int animatedTileIndex, int staticTileIndex)
This assigns the supplied tile index to the animated tile. All cells that have the supplied
animated tile will now display the given tile.
If you need to retrieve the current tile associated with an animated tile, just pass the
animated tile index to
getAnimatedTile()
.
Using Sprites
While a
TiledLayer
uses a palette of tiles to fill a large area, a
Sprite
uses a palette of tiles to
animate a layer that is the same size as a single tile. Usually a
Sprite
represents one of the
protagonists in a game. In
Sprite
parlance, tiles are called
frames
instead. As with a
TiledLayer
,
a
Sprite
is created from a source image that is divided into equally sized frames.
public Sprite(Image image, int frameWidth, int frameHeight)
There's also a special caseāif the image contains just one frame, it will not be animated:
public Sprite(Image image)
Interestingly, a
Sprite
cannot be created from separate frame images; the frames must be
packed into a single source image (this packing is better for compression).
If you want to change the source image after the
Sprite
is created, use
setImage()
:
public void setImage(Image img, int frameWidth, int frameHeight)
The total number of frames contained in the
Sprite
is returned from
getRawFrameCount()
.
Like any other
Layer
, Sprites are rendered when the
paint()
method is called. Usually
the
Sprite
will belong to a
LayerManager
, in which case it is rendered automatically when the
LayerManager
is rendered.
Animating Sprites
Sprite animation is all about
frame sequences
. When a
Sprite
is created, it has a default frame
sequence that includes every frame in the source image. For example, consider the source
image for a fictional character named Dr. Quatsch, shown in Figure 14-5.
Figure 14-5.
A sprite source image
Search WWH ::
Custom Search