![]() |
AnimationWorld: Sprites |
To create sprites, we extend the Sprite
class.
The general process for creating objects map to the following steps
for creating sprites:
sprite
methods.
protected void drawState(Graphics g);
protected void updateState();
protected void resetState();
Sprite
methods to help with debugging.
public String className();
public String toString();
public String getState();
toString
,
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:
setBounds(int width, int height)
method.
See this class for an example.