AnimationWorld -- Sprites
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.
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. We need instance variables
for both the initial and current positions of the Sprite.
- Writing constructors.
- Writing three required
Sprite
methods.
protected void drawState(Graphics g);
Describes how to draw the Sprite in 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.
public String toString();
Returns a String representation of the Sprite.
public String getState();
Returns a String representation of the current state of the Sprite.
Sprites have some information about their environment. They are
- width Tells the Sprite the width of the animation frame.
- height Tells the Sprite the height of the animation frame.
- getStateNumber Tells the Sprite how many times it has updated. Starts at 1.
- getName Tells the Sprite the name the user assigned to it.
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
HTML pages