Class Sprite

  • All Implemented Interfaces:
    Updatable

    public class Sprite
    extends java.lang.Object
    implements Updatable
    Static or animated two-dimensional image that can be integrated into a larger scene. Multiple sprites may use the same image data, the sprite is merely an instance of the image that can be drawn by the renderer and does not modify the image itself. Sprites can be transformed (rotated, scaled) before they are displayed.

    Sprites can have multiple possible graphical states, with each state being represented by either a static image or by an animation.

    • Constructor Summary

      Constructors 
      Constructor Description
      Sprite()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addState​(java.lang.String name, Animation graphics)
      Adds a new animation state to this sprite.
      void addState​(java.lang.String name, Image stateGraphics)
      Shorthand that adds a new state to this sprite that consists of a single static image.
      void changeState​(java.lang.String stateName)
      Changes the sprite's graphics to the state with the specified name.
      Sprite copy()
      Creates a new sprite with states and graphics based on this one, but it starts back in its initial state.
      java.util.Set<java.lang.String> getAvailableStates()  
      Image getCurrentGraphics()  
      int getCurrentHeight()  
      java.lang.String getCurrentState()  
      int getCurrentWidth()  
      Animation getStateGraphics​(java.lang.String stateName)  
      float getTimeInCurrentState()  
      boolean hasState​(java.lang.String stateName)  
      void resetState()
      Leaves the sprite in its current state, but resets the graphics for that state to play from the beginning.
      void update​(float deltaTime)
      Updates this object for the current frame.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Sprite

        public Sprite()
    • Method Detail

      • addState

        public void addState​(java.lang.String name,
                             Animation graphics)
        Adds a new animation state to this sprite.
        Throws:
        java.lang.IllegalArgumentException - if a state with the same name has already been registered with this sprite.
      • addState

        public void addState​(java.lang.String name,
                             Image stateGraphics)
        Shorthand that adds a new state to this sprite that consists of a single static image.
        Throws:
        java.lang.IllegalArgumentException - if a state with the same name has already been registered with this sprite.
      • update

        public void update​(float deltaTime)
        Description copied from interface: Updatable
        Updates this object for the current frame.
        Specified by:
        update in interface Updatable
        Parameters:
        deltaTime - Elapsed time since the last frame, in seconds.
      • changeState

        public void changeState​(java.lang.String stateName)
        Changes the sprite's graphics to the state with the specified name. If the sprite was already in that stae this method does nothing.
      • resetState

        public void resetState()
        Leaves the sprite in its current state, but resets the graphics for that state to play from the beginning.
      • getCurrentState

        public java.lang.String getCurrentState()
      • getTimeInCurrentState

        public float getTimeInCurrentState()
      • getAvailableStates

        public java.util.Set<java.lang.String> getAvailableStates()
      • hasState

        public boolean hasState​(java.lang.String stateName)
      • getStateGraphics

        public Animation getStateGraphics​(java.lang.String stateName)
      • getCurrentGraphics

        public Image getCurrentGraphics()
      • getCurrentWidth

        public int getCurrentWidth()
      • getCurrentHeight

        public int getCurrentHeight()
      • copy

        public Sprite copy()
        Creates a new sprite with states and graphics based on this one, but it starts back in its initial state.