Class Sprite

java.lang.Object
nl.colorize.multimedialib.stage.Sprite
All Implemented Interfaces:
StageNode2D

public class Sprite extends Object implements StageNode2D
Static or animated image that can be displayed on the stage. Multiple sprites can use the same image data. As sprites represent an instance of the original image, sprites can be transformed without affecting or modifying the underlying image data.

Sprites support multiple graphical states, where each state can be identified by its name. The currently active graphics are updated automatically for as long as the sprite is on the stage.

  • Constructor Details

    • Sprite

      public Sprite()
      Creates a sprite without default graphics. Trying to use the sprite before graphics have been added will result in an exception.
    • Sprite

      public Sprite(Animation anim)
      Creates a sprite that uses the specified animation as its default graphics.
    • Sprite

      public Sprite(Image image)
      Creates a sprite that uses the specified image as its default graphics.
  • Method Details

    • addGraphics

      public void addGraphics(String stateName, Animation graphics)
      Adds graphics to this sprite. If the sprite does not contain graphics yet, this will automatically change the sprite's current graphics. Otherwise, changing the sprite's graphics can be done later using the specified name.
      Throws:
      IllegalArgumentException - if a state with the same name has already been registered with this sprite.
    • addGraphics

      public void addGraphics(String stateName, Image stateGraphics)
      Adds graphics to this sprite. If the sprite does not contain graphics yet, this will automatically change the sprite's current graphics. Otherwise, changing the sprite's graphics can be done later using the specified name.
      Throws:
      IllegalArgumentException - if a state with the same name has already been registered with this sprite.
    • changeGraphics

      public void changeGraphics(String stateName)
      Changes this sprite's graphics to the state identified by the specified name. If the sprite is already in that state, this method does nothing.
      Throws:
      IllegalArgumentException - if the sprite does not define any graphics for the requested state.
    • resetCurrentGraphics

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

      public String getActiveState()
    • getPossibleStates

      public Set<String> getPossibleStates()
    • hasGraphics

      public boolean hasGraphics(String stateName)
    • getGraphics

      public Animation getGraphics(String stateName)
    • getCurrentStateGraphics

      @Deprecated public Animation getCurrentStateGraphics()
      Deprecated.
    • getCurrentStateTimer

      @Deprecated public Timer getCurrentStateTimer()
      Deprecated.
    • getCurrentGraphics

      public Image getCurrentGraphics()
    • getCurrentWidth

      public int getCurrentWidth()
    • getCurrentHeight

      public int getCurrentHeight()
    • animate

      public void animate(Timer sceneTime)
      Description copied from interface: StageNode2D
      Called by the renderer at the end of frame updates, while rendering the stage. animationTimer contains the elapsed time since the currently active scene was started. This allows animations to display correctly, without the need to update every single node during each frame update.
      Specified by:
      animate in interface StageNode2D
    • getStageBounds

      public Rect getStageBounds()
      Description copied from interface: StageNode2D
      Returns the bounding box currently occopied by this node, relative to the stage. This method uses StageNode2D.getGlobalTransform().
      Specified by:
      getStageBounds in interface StageNode2D
    • copy

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

      public String toString()
      Overrides:
      toString in class Object
    • getTransform

      public Transform getTransform()
      Description copied from interface: StageNode2D
      Returns this node's local transform, which is interpreted relative to its parent node. The local transform can be modified by application code to modify how and where the node is displayed.
      Specified by:
      getTransform in interface StageNode2D
    • getGlobalTransform

      public Transform getGlobalTransform()
      Description copied from interface: StageNode2D
      Returns this node's global transform, which is interpreted relative to the stage. The global transform is updated by the renderer when drawing the stage at the end of frame updates. Recalculating the global transform is relatively expensive, and is therefore only done once per frame. This means that any changes made to a node's local transform since the last frame update may not yet been reflected in the current state of its global transform.
      Specified by:
      getGlobalTransform in interface StageNode2D