libgdx API

com.badlogic.gdx.graphics.g2d
Class Sprite

java.lang.Object
  extended by com.badlogic.gdx.graphics.g2d.TextureRegion
      extended by com.badlogic.gdx.graphics.g2d.Sprite
Direct Known Subclasses:
TextureAtlas.AtlasSprite

public class Sprite
extends TextureRegion

Holds the geometry, color, and texture information for drawing 2D sprites using SpriteBatch. 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 SpriteBatch.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.

Author:
mzechner, Nathan Sweet

Field Summary
static int C1
           
static int C2
           
static int C3
           
static int C4
           
static int U1
           
static int U2
           
static int U3
           
static int U4
           
static int V1
           
static int V2
           
static int V3
           
static int V4
           
static int X1
           
static int X2
           
static int X3
           
static int X4
           
static int Y1
           
static int Y2
           
static int Y3
           
static int Y4
           
 
Constructor Summary
Sprite()
          Creates an uninitialized sprite.
Sprite(Sprite sprite)
          Creates a sprite that is a copy in every way of the specified sprite.
Sprite(Texture texture)
          Creates a sprite with width, height, and texture region equal to the size of the texture.
Sprite(Texture texture, int srcWidth, int srcHeight)
          Creates a sprite with width, height, and texture region equal to the specified size.
Sprite(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(TextureRegion region)
           
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
 void draw(SpriteBatch spriteBatch)
           
 void draw(SpriteBatch spriteBatch, float alphaModulation)
           
 void flip(boolean x, boolean y)
           
 Rectangle getBoundingRectangle()
          Returns the bounding axis aligned Rectangle that bounds this sprite.
 Color getColor()
          Returns the color of this sprite.
 float getHeight()
           
 float getOriginX()
           
 float getOriginY()
           
 float getRotation()
           
 float getScaleX()
           
 float getScaleY()
           
 float[] getVertices()
          Returns the packed vertices, colors, and texture coordinates for this sprite.
 float getWidth()
           
 float getX()
           
 float getY()
           
 void rotate(float degrees)
          Sets the sprite's rotation 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)
           
 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 setColor(Color tint)
           
 void setColor(float r, float g, float b, float a)
           
 void setOrigin(float originX, float originY)
          Sets the origin in relation to the sprite's position for scaling and rotation.
 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)
           
 void setScale(float scaleXY)
           
 void setScale(float scaleX, float scaleY)
           
 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 translate(float xAmount, float yAmount)
          Sets the position relative to the current position where the sprite will be drawn.
 
Methods inherited from class com.badlogic.gdx.graphics.g2d.TextureRegion
getRegionHeight, getRegionWidth, getRegionX, getRegionY, getTexture, getU, getU2, getV, getV2, setRegion, setRegion, setRegion, setRegion, setRegionHeight, setRegionWidth, setRegionX, setRegionY, setTexture, split, split
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

X1

public static final int X1
See Also:
Constant Field Values

Y1

public static final int Y1
See Also:
Constant Field Values

C1

public static final int C1
See Also:
Constant Field Values

U1

public static final int U1
See Also:
Constant Field Values

V1

public static final int V1
See Also:
Constant Field Values

X2

public static final int X2
See Also:
Constant Field Values

Y2

public static final int Y2
See Also:
Constant Field Values

C2

public static final int C2
See Also:
Constant Field Values

U2

public static final int U2
See Also:
Constant Field Values

V2

public static final int V2
See Also:
Constant Field Values

X3

public static final int X3
See Also:
Constant Field Values

Y3

public static final int Y3
See Also:
Constant Field Values

C3

public static final int C3
See Also:
Constant Field Values

U3

public static final int U3
See Also:
Constant Field Values

V3

public static final int V3
See Also:
Constant Field Values

X4

public static final int X4
See Also:
Constant Field Values

Y4

public static final int Y4
See Also:
Constant Field Values

C4

public static final int C4
See Also:
Constant Field Values

U4

public static final int U4
See Also:
Constant Field Values

V4

public static final int V4
See Also:
Constant Field Values
Constructor Detail

Sprite

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


Sprite

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


Sprite

public Sprite(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. * @param srcWidth The width of the texture region. May be negative to flip the sprite when drawn.

Parameters:
srcHeight - The height of the texture region. May be negative to flip the sprite when drawn.

Sprite

public Sprite(Texture texture,
              int srcX,
              int srcY,
              int srcWidth,
              int srcHeight)
Creates a sprite with width, height, and texture region equal to the specified size. * @param srcWidth The width of the texture region. May be negative to flip the sprite when drawn.

Parameters:
srcHeight - The height of the texture region. May be negative to flip the sprite when drawn.

Sprite

public Sprite(TextureRegion region)

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 Detail

set

public void set(Sprite 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).


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.


setColor

public void setColor(Color tint)

setColor

public void setColor(float r,
                     float g,
                     float b,
                     float a)

setOrigin

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


setRotation

public void setRotation(float degrees)

rotate

public void rotate(float degrees)
Sets the sprite's rotation relative to the current rotation.


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)

setScale

public void setScale(float scaleX,
                     float scaleY)

scale

public void scale(float amount)
Sets the sprite's scale relative to the current scale.


getVertices

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


getBoundingRectangle

public Rectangle getBoundingRectangle()
Returns the bounding axis aligned Rectangle that bounds this sprite. The rectangles x and y coordinates describe its bottom left corner.

Returns:
the bounding Rectangle

draw

public void draw(SpriteBatch spriteBatch)

draw

public void draw(SpriteBatch spriteBatch,
                 float alphaModulation)

getX

public float getX()

getY

public float getY()

getWidth

public float getWidth()

getHeight

public float getHeight()

getOriginX

public float getOriginX()

getOriginY

public float getOriginY()

getRotation

public float getRotation()

getScaleX

public float getScaleX()

getScaleY

public float getScaleY()

getColor

public Color getColor()
Returns the color of this sprite. Changing the returned color will have no affect, setColor(Color) or setColor(float, float, float, float) must be used.


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

flip

public void flip(boolean x,
                 boolean y)
Overrides:
flip in class TextureRegion

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.

libgdx API

Copyright 2010 Mario Zechner (contact@badlogicgames.com), Nathan Sweet (admin@esotericsoftware.com)