Class Sprite

Direct Known Subclasses:
TextureAtlas.AtlasSprite

public class Sprite extends TextureRegion
Holds the geometry, color, and texture information for drawing 2D sprites using Batch. A Sprite has a position and a size given as width and height. The position is relative to the origin of the coordinate system specified via Batch.begin() and the respective matrices. A Sprite is always rectangular and its position (x, y) are located in the bottom left corner of that rectangle. A Sprite also has an origin around which rotations and scaling are performed (that is, the origin is not modified by rotation and scaling). The origin is given relative to the bottom left corner of the Sprite, its position.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an uninitialized sprite.
    Sprite(com.badlogic.gdx.graphics.Texture texture)
    Creates a sprite with width, height, and texture region equal to the size of the texture.
    Sprite(com.badlogic.gdx.graphics.Texture texture, int srcWidth, int srcHeight)
    Creates a sprite with width, height, and texture region equal to the specified size.
    Sprite(com.badlogic.gdx.graphics.Texture texture, int srcX, int srcY, int srcWidth, int srcHeight)
    Creates a sprite with width, height, and texture region equal to the specified size.
    Sprite(Sprite sprite)
    Creates a sprite that is a copy in every way of the specified sprite.
    Creates a sprite based on a specific TextureRegion, the new sprite's region is a copy of the parameter region - altering one does not affect the other
    Sprite(TextureRegion region, int srcX, int srcY, int srcWidth, int srcHeight)
    Creates a sprite with width, height, and texture region equal to the specified size, relative to specified sprite's texture region.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    draw(com.badlogic.gdx.graphics.g2d.Batch batch)
     
    void
    draw(com.badlogic.gdx.graphics.g2d.Batch batch, float alphaModulation)
     
    void
    flip(boolean x, boolean y)
    boolean parameters x,y are not setting a state, but performing a flip
    com.badlogic.gdx.math.Rectangle
    Returns the bounding axis aligned Rectangle that bounds this sprite.
    com.badlogic.gdx.graphics.Color
    Returns the color of this sprite.
    float
     
    float
    The origin influences setPosition(float, float), setRotation(float) and the expansion direction of scaling setScale(float, float)
    float
    The origin influences setPosition(float, float), setRotation(float) and the expansion direction of scaling setScale(float, float)
    float
     
    float
    X scale of the sprite, independent of size set by setSize(float, float)
    float
    Y scale of the sprite, independent of size set by setSize(float, float)
    float[]
    Returns the packed vertices, colors, and texture coordinates for this sprite.
    float
     
    float
     
    float
     
    void
    rotate(float degrees)
    Sets the sprite's rotation in degrees relative to the current rotation.
    void
    rotate90(boolean clockwise)
    Rotates this sprite 90 degrees in-place by rotating the texture coordinates.
    void
    scale(float amount)
    Sets the sprite's scale relative to the current scale.
    void
    scroll(float xAmount, float yAmount)
    Offsets the region relative to the current region.
    void
    set(Sprite sprite)
    Make this sprite a copy in every way of the specified sprite
    void
    setAlpha(float a)
    Sets the alpha portion of the color used to tint this sprite.
    void
    setBounds(float x, float y, float width, float height)
    Sets the position and size of the sprite when drawn, before scaling and rotation are applied.
    void
    setCenter(float x, float y)
    Sets the position so that the sprite is centered on (x, y)
    void
    setCenterX(float x)
    Sets the x position so that it is centered on the given x parameter
    void
    setCenterY(float y)
    Sets the y position so that it is centered on the given y parameter
    void
    setColor(float r, float g, float b, float a)
     
    void
    setColor(com.badlogic.gdx.graphics.Color tint)
    Sets the color used to tint this sprite.
    void
    setFlip(boolean x, boolean y)
    Set the sprite's flip state regardless of current condition
    void
    setOrigin(float originX, float originY)
    Sets the origin in relation to the sprite's position for scaling and rotation.
    void
    setOriginBasedPosition(float x, float y)
    Sets the position where the sprite will be drawn, relative to its current origin.
    void
    Place origin in the center of the sprite
    void
    setPackedColor(float packedColor)
    Sets the color of this sprite, expanding the alpha from 0-254 to 0-255.
    void
    setPosition(float x, float y)
    Sets the position where the sprite will be drawn.
    void
    setRegion(float u, float v, float u2, float v2)
     
    void
    setRotation(float degrees)
    Sets the rotation of the sprite in degrees.
    void
    setScale(float scaleXY)
    Sets the sprite's scale for both X and Y uniformly.
    void
    setScale(float scaleX, float scaleY)
    Sets the sprite's scale for both X and Y.
    void
    setSize(float width, float height)
    Sets the size of the sprite when drawn, before scaling and rotation are applied.
    void
    setU(float u)
     
    void
    setU2(float u2)
     
    void
    setV(float v)
     
    void
    setV2(float v2)
     
    void
    setX(float x)
    Sets the x position where the sprite will be drawn.
    void
    setY(float y)
    Sets the y position where the sprite will be drawn.
    void
    translate(float xAmount, float yAmount)
    Sets the position relative to the current position where the sprite will be drawn.
    void
    translateX(float xAmount)
    Sets the x position relative to the current position where the sprite will be drawn.
    void
    translateY(float yAmount)
    Sets the y position relative to the current position where the sprite will be drawn.

    Methods inherited from class java.lang.Object

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

    • Sprite

      public Sprite()
      Creates an uninitialized sprite. The sprite will need a texture region and bounds set before it can be drawn.
    • Sprite

      public Sprite(com.badlogic.gdx.graphics.Texture texture)
      Creates a sprite with width, height, and texture region equal to the size of the texture.
    • Sprite

      public Sprite(com.badlogic.gdx.graphics.Texture texture, int srcWidth, int srcHeight)
      Creates a sprite with width, height, and texture region equal to the specified size. The texture region's upper left corner will be 0,0.
      Parameters:
      srcWidth - The width of the texture region. May be negative to flip the sprite when drawn.
      srcHeight - The height of the texture region. May be negative to flip the sprite when drawn.
    • Sprite

      public Sprite(com.badlogic.gdx.graphics.Texture texture, int srcX, int srcY, int srcWidth, int srcHeight)
      Creates a sprite with width, height, and texture region equal to the specified size.
      Parameters:
      srcWidth - The width of the texture region. May be negative to flip the sprite when drawn.
      srcHeight - The height of the texture region. May be negative to flip the sprite when drawn.
    • Sprite

      public Sprite(TextureRegion region)
      Creates a sprite based on a specific TextureRegion, the new sprite's region is a copy of the parameter region - altering one does not affect the other
    • Sprite

      public Sprite(TextureRegion region, int srcX, int srcY, int srcWidth, int srcHeight)
      Creates a sprite with width, height, and texture region equal to the specified size, relative to specified sprite's texture region.
      Parameters:
      srcWidth - The width of the texture region. May be negative to flip the sprite when drawn.
      srcHeight - The height of the texture region. May be negative to flip the sprite when drawn.
    • Sprite

      public Sprite(Sprite sprite)
      Creates a sprite that is a copy in every way of the specified sprite.
  • Method Details

    • set

      public void set(Sprite sprite)
      Make this sprite a copy in every way of the specified sprite
    • setBounds

      public void setBounds(float x, float y, float width, float height)
      Sets the position and size of the sprite when drawn, before scaling and rotation are applied. If origin, rotation, or scale are changed, it is slightly more efficient to set the bounds after those operations.
    • setSize

      public void setSize(float width, float height)
      Sets the size of the sprite when drawn, before scaling and rotation are applied. If origin, rotation, or scale are changed, it is slightly more efficient to set the size after those operations. If both position and size are to be changed, it is better to use setBounds(float, float, float, float).
    • setPosition

      public void setPosition(float x, float y)
      Sets the position where the sprite will be drawn. If origin, rotation, or scale are changed, it is slightly more efficient to set the position after those operations. If both position and size are to be changed, it is better to use setBounds(float, float, float, float).
    • setOriginBasedPosition

      public void setOriginBasedPosition(float x, float y)
      Sets the position where the sprite will be drawn, relative to its current origin.
    • setCenterX

      public void setCenterX(float x)
      Sets the x position so that it is centered on the given x parameter
    • setCenterY

      public void setCenterY(float y)
      Sets the y position so that it is centered on the given y parameter
    • setCenter

      public void setCenter(float x, float y)
      Sets the position so that the sprite is centered on (x, y)
    • translateX

      public void translateX(float xAmount)
      Sets the x position relative to the current position where the sprite will be drawn. If origin, rotation, or scale are changed, it is slightly more efficient to translate after those operations.
    • translateY

      public void translateY(float yAmount)
      Sets the y position relative to the current position where the sprite will be drawn. If origin, rotation, or scale are changed, it is slightly more efficient to translate after those operations.
    • translate

      public void translate(float xAmount, float yAmount)
      Sets the position relative to the current position where the sprite will be drawn. If origin, rotation, or scale are changed, it is slightly more efficient to translate after those operations.
    • setAlpha

      public void setAlpha(float a)
      Sets the alpha portion of the color used to tint this sprite.
    • setColor

      public void setColor(float r, float g, float b, float a)
      See Also:
    • setPackedColor

      public void setPackedColor(float packedColor)
      Sets the color of this sprite, expanding the alpha from 0-254 to 0-255.
      See Also:
    • setOrigin

      public void setOrigin(float originX, float originY)
      Sets the origin in relation to the sprite's position for scaling and rotation.
    • setOriginCenter

      public void setOriginCenter()
      Place origin in the center of the sprite
    • getRotation

      public float getRotation()
      Returns:
      the rotation of the sprite in degrees
    • setRotation

      public void setRotation(float degrees)
      Sets the rotation of the sprite in degrees. Rotation is centered on the origin set in setOrigin(float, float)
    • rotate

      public void rotate(float degrees)
      Sets the sprite's rotation in degrees relative to the current rotation. Rotation is centered on the origin set in setOrigin(float, float)
    • rotate90

      public void rotate90(boolean clockwise)
      Rotates this sprite 90 degrees in-place by rotating the texture coordinates. This rotation is unaffected by setRotation(float) and rotate(float).
    • setScale

      public void setScale(float scaleXY)
      Sets the sprite's scale for both X and Y uniformly. The sprite scales out from the origin. This will not affect the values returned by getWidth() and getHeight()
    • setScale

      public void setScale(float scaleX, float scaleY)
      Sets the sprite's scale for both X and Y. The sprite scales out from the origin. This will not affect the values returned by getWidth() and getHeight()
    • scale

      public void scale(float amount)
      Sets the sprite's scale relative to the current scale. for example: original scale 2 -> sprite.scale(4) -> final scale 6. The sprite scales out from the origin. This will not affect the values returned by getWidth() and getHeight()
    • getVertices

      public float[] getVertices()
      Returns the packed vertices, colors, and texture coordinates for this sprite.
    • getBoundingRectangle

      public com.badlogic.gdx.math.Rectangle getBoundingRectangle()
      Returns the bounding axis aligned Rectangle that bounds this sprite. The rectangles x and y coordinates describe its bottom left corner. If you change the position or size of the sprite, you have to fetch the triangle again for it to be recomputed.
      Returns:
      the bounding Rectangle
    • draw

      public void draw(com.badlogic.gdx.graphics.g2d.Batch batch)
    • draw

      public void draw(com.badlogic.gdx.graphics.g2d.Batch batch, float alphaModulation)
    • getX

      public float getX()
    • setX

      public void setX(float x)
      Sets the x position where the sprite will be drawn. If origin, rotation, or scale are changed, it is slightly more efficient to set the position after those operations. If both position and size are to be changed, it is better to use setBounds(float, float, float, float).
    • getY

      public float getY()
    • setY

      public void setY(float y)
      Sets the y position where the sprite will be drawn. If origin, rotation, or scale are changed, it is slightly more efficient to set the position after those operations. If both position and size are to be changed, it is better to use setBounds(float, float, float, float).
    • getWidth

      public float getWidth()
      Returns:
      the width of the sprite, not accounting for scale.
    • getHeight

      public float getHeight()
      Returns:
      the height of the sprite, not accounting for scale.
    • getOriginX

      public float getOriginX()
      The origin influences setPosition(float, float), setRotation(float) and the expansion direction of scaling setScale(float, float)
    • getOriginY

      public float getOriginY()
      The origin influences setPosition(float, float), setRotation(float) and the expansion direction of scaling setScale(float, float)
    • getScaleX

      public float getScaleX()
      X scale of the sprite, independent of size set by setSize(float, float)
    • getScaleY

      public float getScaleY()
      Y scale of the sprite, independent of size set by setSize(float, float)
    • getColor

      public com.badlogic.gdx.graphics.Color getColor()
      Returns the color of this sprite. If the returned instance is manipulated, setColor(Color) must be called afterward.
    • setColor

      public void setColor(com.badlogic.gdx.graphics.Color tint)
      Sets the color used to tint this sprite. Default is Color.WHITE.
    • setRegion

      public void setRegion(float u, float v, float u2, float v2)
      Overrides:
      setRegion in class TextureRegion
    • setU

      public void setU(float u)
      Overrides:
      setU in class TextureRegion
    • setV

      public void setV(float v)
      Overrides:
      setV in class TextureRegion
    • setU2

      public void setU2(float u2)
      Overrides:
      setU2 in class TextureRegion
    • setV2

      public void setV2(float v2)
      Overrides:
      setV2 in class TextureRegion
    • setFlip

      public void setFlip(boolean x, boolean y)
      Set the sprite's flip state regardless of current condition
      Parameters:
      x - the desired horizontal flip state
      y - the desired vertical flip state
    • flip

      public void flip(boolean x, boolean y)
      boolean parameters x,y are not setting a state, but performing a flip
      Overrides:
      flip in class TextureRegion
      Parameters:
      x - perform horizontal flip
      y - perform vertical flip
    • scroll

      public void scroll(float xAmount, float yAmount)
      Description copied from class: TextureRegion
      Offsets the region relative to the current region. Generally the region's size should be the entire size of the texture in the direction(s) it is scrolled.
      Overrides:
      scroll in class TextureRegion
      Parameters:
      xAmount - The percentage to offset horizontally.
      yAmount - The percentage to offset vertically. This is done in texture space, so up is negative.