java.lang.Object | ||
↳ | com.neomades.game.Layer | |
↳ | com.neomades.game.Sprite |
A Sprite is a basic visual element that can be rendered with one of several frames stored in an Image; different frames can be shown to animate the Sprite. Several transforms such as flipping and rotation can also be applied to a Sprite to further vary its appearance. As with all Layer subclasses, a Sprite's location can be changed and it can also be made visible or invisible.
The raw frames used to render a Sprite are provided in a single Image object, which may be mutable or immutable. If more than one frame is used, the Image is broken up into a series of equally-sized frames of a specified width and height. As shown in the figure below, the same set of frames may be stored in several different arrangements depending on what is the most convenient for the game developer.
[Expand]
Inherited Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Sprite(Image image)
Creates a new non-animated Sprite using the provided Image.
| |||||||||||
Sprite(Sprite s)
Creates a new Sprite from another Sprite.
| |||||||||||
Sprite(Image image, int frameWidth, int frameHeight)
Creates a new animated Sprite using frames contained in the provided Image.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void |
defineReferencePixel(int x, int y)
Defines the reference pixel for this Sprite.
| ||||||||||
int |
getFrame()
Gets the current index in the frame sequence.
| ||||||||||
int |
getRawFrameCount()
Gets the number of raw frames for this Sprite.
| ||||||||||
int |
nextFrame()
Selects the next frame in the frame sequence.
| ||||||||||
void |
paint(Graphics g)
Draws the Sprite.
| ||||||||||
int |
previousFrame()
Selects the previous frame in the frame sequence.
| ||||||||||
int |
setFrame(int frame)
Selects the current frame in the frame sequence.
| ||||||||||
void |
setImage(Image image, int frameWidth, int frameHeight)
Changes the Image containing the Sprite's frames.
| ||||||||||
void |
setTransform(int transformOrientation)
Sets the transform for this Sprite.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() |
Creates a new non-animated Sprite using the provided Image.
This constructor is functionally equivalent to calling new Sprite(image, image.getWidth(), image.getHeight())
By default, the Sprite is visible and its upper-left corner is positioned at (0,0) in the painter's coordinate system.
image | the Image to use as the single frame for the Sprite |
---|
Creates a new Sprite from another Sprite.
All instance attributes (raw frames, position, frame sequence, current frame, reference point, collision rectangle, transform, and visibility) of the source Sprite are duplicated in the new Sprite.
s | the Sprite to create a copy of |
---|
Creates a new animated Sprite using frames contained in the provided Image.
image | the Image to use for Sprite |
---|---|
frameWidth | the width, in pixels, of the individual raw frames |
frameHeight | the height, in pixels, of the individual raw frames |
Defines the reference pixel for this Sprite. The pixel is defined by its location relative to the upper-left corner of the Sprite's un-transformed frame, and it may lay outside of the frame's bounds.
When a transformation is applied, the reference pixel is defined relative to the Sprite's initial upper-left corner before transformation. This corner may no longer appear as the upper-left corner in the painter's coordinate system under current transformation.
By default, a Sprite's reference pixel is located at (0,0); that is, the pixel in the upper-left corner of the raw frame.
Changing the reference pixel does not change the Sprite's physical position
in the painter's coordinate system; that is, the values returned by
getX()
and getY()
will not change as a result of defining
the reference pixel. However, subsequent calls to methods that involve the
reference pixel will be impacted by its new definition
x | the horizontal location of the reference pixel, relative to the left edge of the un-transformed frame |
---|---|
y | the vertical location of the reference pixel, relative to the top edge of the un-transformed frame |
Gets the current index in the frame sequence.
The index returned refers to the current entry in the frame sequence, not the index of the actual frame that is displayed.
Gets the number of raw frames for this Sprite. The value returned reflects the number of frames; it does not reflect the length of the Sprite's frame sequence. However, these two values will be the same if the default frame sequence is used.
Selects the next frame in the frame sequence.
The frame sequence is considered to be circular, i.e. if nextFrame() is called when at the end of the sequence, this method will advance to the first entry in the sequence.
Draws the Sprite.
Draws current frame of Sprite using the provided Graphics object. The
Sprite's upper left corner is rendered at the Sprite's current position
relative to the origin of the Graphics object. The current position of the
Sprite's upper-left corner can be retrieved by calling getX()
and
getY()
.
Rendering is subject to the clip region of the Graphics object. The Sprite will be drawn only if it is visible.
If the Sprite's Image is mutable, the Sprite is rendered using the current contents of the Image.
g | the graphics object to draw Sprite on |
---|
Selects the previous frame in the frame sequence.
The frame sequence is considered to be circular, i.e. if prevFrame() is called when at the start of the sequence, this method will advance to the last entry in the sequence.
Selects the current frame in the frame sequence.
The current frame is rendered when paint(Graphics)
is called.
The index provided refers to the desired entry in the frame sequence, not the index of the actual frame itself.
frame | the index of of the desired entry in the frame sequence |
---|
IndexOutOfBoundsException | if frameIndex is less than 0 |
---|---|
IndexOutOfBoundsException | if frameIndex is equal to or greater than the length of the current frame sequence (or the number of raw frames for the default sequence) |
Changes the Image containing the Sprite's frames.
Replaces the current raw frames of the Sprite with a new set of raw frames.
See the constructor Sprite(Image, int, int)
for information on how
the frames are created from the image. The values returned by
getWidth()
and getHeight()
will reflect the new frame width
and frame height subject to the Sprite's current transform.
The reference point location is unchanged as a result of calling this method, both in terms of its defined location within the Sprite and its position in the painter's coordinate system. However, if the frame size is changed and the Sprite has been transformed, the position of the Sprite's upper-left corner may change such that the reference point remains stationary.
If the Sprite's frame size is changed by this method, the collision rectangle is reset to its default value (i.e. it is set to the new bounds of the untransformed Sprite).
image | the Image to use for Sprite |
---|---|
frameWidth | the width in pixels of the individual raw frame |
frameHeight | the height in pixels of the individual raw frames |
Sets the transform for this Sprite. Transforms can be applied to a Sprite to
change its rendered appearance. Transforms are applied to the original Sprite
image; they are not cumulative, nor can they be combined. By default, a
Sprite's transform is NONE
.
Since some transforms involve rotations of 90 or 270 degrees, their use may
result in the overall width and height of the Sprite being swapped. As a
result, the values returned by getWidth()
and getHeight()
may change.
The collision rectangle is also modified by the transform so that it remains static relative to the pixel data of the Sprite. Similarly, the defined reference pixel is unchanged by this method, but its visual location within the Sprite may change as a result.
This method repositions the Sprite so that the location of the reference pixel in the painter's coordinate system does not change as a result of changing the transform.
transformOrientation | the desired transform for this Sprite |
---|
IllegalArgumentException | if the requested transform is invalid |
---|