The process of creating sprites is similar to the process for designing
and creating any kind of objects in Java. We need to decide what our
objects will do, what information we need to keep track of, write constructor
methods so people can create instances of our class, write methods so
the objects actually do something, and write methods that tell us the
state of the object which helps us debug the object.
AnimationWorld
Sprites
Animations
Animation Players
To create sprites, we extend the Sprite
class.
The general process for creating objects map to the following steps
for creating sprites:
- Deciding what the sprite will look like and how it will move.
- Deciding what information is needed to keep track of the state of the sprite. This information will be stored in instance variables. Instance variables characterize the current state of the sprite and also specify any initial values for state variables that we wish to reset.
- Writing constructor methods for the sprite.
- Writing three required
sprite
methods.protected void drawState(Graphics g);
Describes how to draw the sprite in the graphics context associated with a particular frame.protected void updateState();
Describes how to update the state of the sprite between frames.protected void resetState();
Describes how to reset the state of the sprite back to its initial state.
- Writing three optional
Sprite
methods to help with debugging.public String className();
Returns the name of the class. By default, this is "Sprite", but the default can be overridden in a subclass.public String toString();
Returns a String representation of the sprite.public String getState();
Returns a String representation of the current state of the sprite. By default, this is the same string returned bytoString
, but this default can be overridden.
Here are the rest of the sprite methods. Usually these methods are invoked by other parts of the animation implementation, but in some cases it is helpful for you to invoke them when making new sprites:protected int width
By default, the width of the frame in which the sprite lives. Can be changed viasetBounds()
.
protected int height
By default, the height of the frame in which the sprite lives. Can be changed viasetBounds()
.
public int getStateNumber()
Returns the current state number of this sprite. The state number is initially 1 and is incremented with every call toupdate()
. An invocation ofreset()
resets the state number to 1.
public String getName()
Individual sprites have a name. This method returns the value of that name.
public String setName(String s)
Changes the name of this sprite to bes
.
public Dimension getBounds()
Returns a Dimension object containing both the width and the height of this sprite.
public void setBounds(Dimension d)
Changes the width and height of this sprite according to dimensiond
.
public void setBounds(int w, int h)
Changes the width of this sprite to bew
and its height to beh
.
public boolean isActive()
Returns the state of this sprite's activity flag, which controls whether this sprite is currently active. Only an active sprite is updated from frame to frame. An inactive sprite may still be visible, however.
public void setActive(boolean b)
Changes the state of the activity flag of this sprite to beb
.
public boolean isVisible()
Returns the state of this sprite's visibility flag, which controls whether this sprite is currently visible. Only an visible sprite is drawn in the frame. An invisible sprite may still be active, however.
public void setVisible(boolean b)
Changes the visibility flag of this sprite to beb
.
public boolean isDebugMode()
Returns the state of this sprite's debug flag, which controls whether debugging information is displayed on every call toupdate()
orreset()
.
public void setDebugMode(boolean b)
Changes the debug flag of this sprite to beb
.
public void update()
Updates an active sprite by incrementing its statenumber and then invokingupdateState()
. This method is invoked by the animation implementation and is not intended to be invoked by users.
public void draw()
Draws a visible sprite by invokingdrawState()
and displaying any debugging information. This method is invoked by the animation implementation and is not intended to be invoked by users.
public void reset()
Resets this sprite by setting its state number to 1 and invokingresetState()
. This method is invoked by the animation implementation and is not intended to be invoked by users.
Below are five examples of sprites:
- ColorBackground Backgrounds are sprites, too! This sprite just colors the entire screen a solid color.
- RightBuggle A Buggle sprite which moves facing right.
- RightLeftBuggle A Buggle sprite which moves left and right. It turns when it hits the side of the animation frame.
- SpyBuggle Look out! This Buggle sees you! An example of creating objects via inheritance. A SpyBuggle is an extension of a RightLeftBuggle.
- SpyArmy How many spies do we want? Now we can have plenty!
An example of creating objects using composition. Composition means creating
an object out of other objects. In this case, a SpyArmy is a sprite made up of
a collection of SpyBuggle sprites. A sprite which is a composition of other sprites
must also override the
setBounds(int width, int height)
method. See this class for an example.
AnimationWorld
Sprites
Animations
Animation Players