GTGE API

com.golden.gamedev.object
Class AnimatedSprite

java.lang.Object
  extended by com.golden.gamedev.object.Sprite
      extended by com.golden.gamedev.object.AnimatedSprite
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
AdvanceSprite

public class AnimatedSprite
extends Sprite

AnimatedSprite class is sprite that use several images and can be animated.

To animate animated sprite, simply use setAnimate(true), and to loop the animation use setLoopAnim(true).
To control animation speed, adjust the sprite animation timer delay.
To control animation frame sequence, use setAnimationFrame(int startframe, int endframe).

Animated sprite usage example :

    AnimatedSprite  sprite;
    BufferedImage[] spriteImage;

    // create the sprite
    sprite = new AnimatedSprite(spriteImage, 0, 0);

    // set animation speed 100 milliseconds for each frame
    sprite.getAnimationTimer().setDelay(100);

    // set animation frame starting from the first image to the third image
    sprite.setAnimationFrame(0, 2);

    // animate the sprite, and perform continous animation
    sprite.setAnimate(true);
    sprite.setLoopAnim(true);
 
AnimatedSprite is only to make a simple animated sprite, there is no property to control sprite animation behaviour. For more control to determine sprite animation based on sprite status and direction, use AdvanceSprite.

See Also:
AdvanceSprite, Serialized Form

Field Summary
 
Fields inherited from class com.golden.gamedev.object.Sprite
height, width
 
Constructor Summary
AnimatedSprite()
          Creates new AnimatedSprite with null image and located at (0, 0).
AnimatedSprite(BufferedImage[] image)
          Creates new AnimatedSprite with specified images and located at (0, 0).
AnimatedSprite(BufferedImage[] image, double x, double y)
          Creates new AnimatedSprite with specified images and location.
AnimatedSprite(double x, double y)
          Creates new AnimatedSprite with null image to specified location.
 
Method Summary
 Timer getAnimationTimer()
          Returns sprite animation timer.
 int getFinishAnimationFrame()
          Returns the finish animation frame of this sprite.
 int getFrame()
          Returns current frame animation.
 BufferedImage getImage()
          Returns image of current frame.
 BufferedImage getImage(int i)
          Returns image of specified frame.
 BufferedImage[] getImages()
          Returns sprite animation images.
 int getStartAnimationFrame()
          Returns the start animation frame of this frame.
 boolean isAnimate()
          Returns whether this sprite is currently animating or not.
 boolean isLoopAnim()
          Returns whether this sprite animation is looping continously or not.
 void render(Graphics2D g, int xs, int ys)
          Renders sprite image to specified graphics context and specified location.
 void setAnimate(boolean b)
          Sets true to animate this sprite.
 void setAnimationFrame(int start, int finish)
          Sets sprite animation sequence, starting from start frame and ended at finish frame.
 void setAnimationTimer(Timer t)
          Sets sprite animation timer.
 void setFrame(int i)
          Sets current frame animation.
 void setImage(BufferedImage image)
          This method is obsolete on animated sprite, since animated sprite always use several images for its animation.
 void setImages(BufferedImage[] image)
          Sets sprite animation images.
 void setLoopAnim(boolean b)
          Sets true to animate this sprite continously.
 void update(long elapsedTime)
          Updates this sprite.
protected  void updateAnimation()
          Updates sprite animation.
 
Methods inherited from class com.golden.gamedev.object.Sprite
addHorizontalSpeed, addVerticalSpeed, forceX, forceY, getBackground, getDataID, getDistance, getHeight, getHorizontalSpeed, getID, getLayer, getOldX, getOldY, getScreenX, getScreenY, getVerticalSpeed, getWidth, getX, getY, isActive, isImmutable, isOnScreen, isOnScreen, move, moveTo, moveX, moveY, render, setActive, setBackground, setDataID, setHorizontalSpeed, setID, setImmutable, setLayer, setLocation, setMovement, setSpeed, setVerticalSpeed, setX, setY, updateMovement
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AnimatedSprite

public AnimatedSprite(BufferedImage[] image,
                      double x,
                      double y)
Creates new AnimatedSprite with specified images and location.

Parameters:
image - sprite images
x - sprite x-coordinate
y - sprite y-coordinate

AnimatedSprite

public AnimatedSprite(BufferedImage[] image)
Creates new AnimatedSprite with specified images and located at (0, 0).

See Also:
Sprite.setLocation(double, double)

AnimatedSprite

public AnimatedSprite(double x,
                      double y)
Creates new AnimatedSprite with null image to specified location.

The sprite images must be set before rendering.

Parameters:
x - sprite x-coordinate
y - sprite y-coordinate

AnimatedSprite

public AnimatedSprite()
Creates new AnimatedSprite with null image and located at (0, 0).

The sprite images must be set before rendering.

See Also:
setImages(BufferedImage[]), Sprite.setLocation(double, double)
Method Detail

setImages

public void setImages(BufferedImage[] image)
Sets sprite animation images.


setImage

public void setImage(BufferedImage image)
This method is obsolete on animated sprite, since animated sprite always use several images for its animation.

Overrides:
setImage in class Sprite
See Also:
setImages(BufferedImage[])

getImages

public BufferedImage[] getImages()
Returns sprite animation images.


getImage

public BufferedImage getImage(int i)
Returns image of specified frame.


getImage

public BufferedImage getImage()
Returns image of current frame.

Overrides:
getImage in class Sprite

setFrame

public void setFrame(int i)
Sets current frame animation.


getFrame

public int getFrame()
Returns current frame animation.


setAnimationFrame

public void setAnimationFrame(int start,
                              int finish)
Sets sprite animation sequence, starting from start frame and ended at finish frame. The sprite current frame is automatically set to start frame.

The first images is frame 0 and the last images is frame getImages().length-1.

For example to make the sprite animated from the first image to the third image :

    AnimatedSprite sprite;

    // animate from first image to third image
    sprite.setAnimationFrame(0, 2);

    sprite.setAnimate(true);
 
Note: If the start and finish frame is same with current animation frame, no action is taken.


getStartAnimationFrame

public int getStartAnimationFrame()
Returns the start animation frame of this frame.

See Also:
setAnimationFrame(int, int)

getFinishAnimationFrame

public int getFinishAnimationFrame()
Returns the finish animation frame of this sprite.

See Also:
setAnimationFrame(int, int)

update

public void update(long elapsedTime)
Description copied from class: Sprite
Updates this sprite.

Overrides:
update in class Sprite

updateAnimation

protected void updateAnimation()
Updates sprite animation.

The animation frame is increased by one, and if the animation frame is exceeded total animation frame, the animation frame is back to the first animation frame, and stop the animation if the animation is not set to loop continously.


render

public void render(Graphics2D g,
                   int xs,
                   int ys)
Description copied from class: Sprite
Renders sprite image to specified graphics context and specified location.

Overrides:
render in class Sprite
Parameters:
g - graphics context
xs - screen x-coordinate
ys - screen y-coordinate

isAnimate

public boolean isAnimate()
Returns whether this sprite is currently animating or not.


setAnimate

public void setAnimate(boolean b)
Sets true to animate this sprite.


isLoopAnim

public boolean isLoopAnim()
Returns whether this sprite animation is looping continously or not.


setLoopAnim

public void setLoopAnim(boolean b)
Sets true to animate this sprite continously.


setAnimationTimer

public void setAnimationTimer(Timer t)
Sets sprite animation timer.


getAnimationTimer

public Timer getAnimationTimer()
Returns sprite animation timer.


GTGE API

Copyright © 2003-2005 Golden T Studios. All rights reserved. Use is subject to license terms.
GoldenStudios.or.id