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:

  1. Deciding what the sprite will look like and how it will move.
  2. 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.
  3. Writing constructor methods for the sprite.
  4. Writing three required sprite methods.
  5. Writing three optional Sprite methods to help with debugging.
In addition to the methods listed above, sprites support a number of additional instance variables and methods. The following are those that are most useful in making your own sprites:
protected int width
By default, the width of the frame in which the sprite lives. Can be changed via setBounds().

protected int height
By default, the height of the frame in which the sprite lives. Can be changed via setBounds().

public int getStateNumber()
Returns the current state number of this sprite. The state number is initially 1 and is incremented with every call to update(). An invocation of reset() 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 be s.

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:
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 dimension d.

public void setBounds(int w, int h)
Changes the width of this sprite to be w and its height to be h.

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 be b.

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 be b.

public boolean isDebugMode()
Returns the state of this sprite's debug flag, which controls whether debugging information is displayed on every call to update() or reset().

public void setDebugMode(boolean b)
Changes the debug flag of this sprite to be b.

public void update()
Updates an active sprite by incrementing its statenumber and then invoking updateState(). 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 invoking drawState() 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 invoking resetState(). This method is invoked by the animation implementation and is not intended to be invoked by users.

Below are five examples of sprites:


AnimationWorld
Sprites
Animations
Animation Players
HTML pages