APIdia Launch Partner
Top Description Fields Constructors Methods
com.badlogic.gdx.graphics.g2d

public Class Sprite

extends TextureRegion
Class Inheritance
Known Direct Subclasses
com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite, com.badlogic.gdx.graphics.g2d.ParticleEmitter.Particle
Static Imports
com.badlogic.gdx.graphics.g2d.SpriteBatch.*

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.
Authors
mzechner, Nathan Sweet

Field Summary

Modifier and TypeField and Description
private Rectangle
private final Color
private boolean
pack-priv float
private float
private float
private float
private float
private float
private float
pack-priv static final int
pack-priv static final int
pack-priv final float[]
pack-priv float
private float
x

private float
y

Inherited from com.badlogic.gdx.graphics.g2d.TextureRegion:
regionHeightregionWidthtextureuu2vv2

Constructor Summary

AccessConstructor and Description
public
Sprite()

Creates an uninitialized sprite.

public
Sprite(Texture texture)

Creates a sprite with width, height, and texture region equal to the size of the texture.

public
Sprite(Texture texture, int
The width of the texture region. May be negative to flip the sprite when drawn.
srcWidth
,
int
The height of the texture region. May be negative to flip the sprite when drawn.
srcHeight
)

Creates a sprite with width, height, and texture region equal to the specified size.

public
Sprite(Texture texture, int srcX, int srcY, int
The width of the texture region. May be negative to flip the sprite when drawn.
srcWidth
,
int
The height of the texture region. May be negative to flip the sprite when drawn.
srcHeight
)

Creates a sprite with width, height, and texture region equal to the specified size.

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

public
Sprite(TextureRegion region, int srcX, int srcY, int
The width of the texture region. May be negative to flip the sprite when drawn.
srcWidth
,
int
The height of the texture region. May be negative to flip the sprite when drawn.
srcHeight
)

Creates a sprite with width, height, and texture region equal to the specified size, relative to specified sprite's texture region.

public
Sprite(Sprite sprite)

Creates a sprite that is a copy in every way of the specified sprite.

Method Summary

Modifier and TypeMethod and Description
public void
draw(Batch batch)

public void
draw(Batch batch, float alphaModulation)

public void
flip(boolean
perform horizontal flip
x
,
boolean
perform vertical flip
y
)

Overrides com.badlogic.gdx.graphics.g2d.TextureRegion.flip.

boolean parameters x,y are not setting a state, but performing a flip

public Rectangle

Returns:

the bounding Rectangle
getBoundingRectangle
()

Returns the bounding axis aligned Rectangle that bounds this sprite.

public Color
getColor()

Returns the color of this sprite.

public float
getHeight()

Returns: the height of the sprite, not accounting for scale.

public float
getOriginX()

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

public float
getOriginY()

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

public float
getPackedColor()

Returns the packed color of this sprite.

public float
getRotation()

Returns: the rotation of the sprite in degrees

public float
getScaleX()

X scale of the sprite, independent of size set by setSize(float, float)

public float
getScaleY()

Y scale of the sprite, independent of size set by setSize(float, float)

public float[]
getVertices()

Returns the packed vertices, colors, and texture coordinates for this sprite.

public float
getWidth()

Returns: the width of the sprite, not accounting for scale.

public float
getX()

public float
getY()

public void
rotate(float degrees)

Sets the sprite's rotation in degrees relative to the current rotation.

public void
rotate90(boolean clockwise)

Rotates this sprite 90 degrees in-place by rotating the texture coordinates.

public void
scale(float amount)

Sets the sprite's scale relative to the current scale.

public void
scroll(float xAmount, float yAmount)

Overrides com.badlogic.gdx.graphics.g2d.TextureRegion.scroll.

Offsets the region relative to the current region.

public void
set(Sprite sprite)

Make this sprite a copy in every way of the specified sprite

public void
setAlpha(float a)

Sets the alpha portion of the color used to tint this sprite.

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.

public void
setCenter(float x, float y)

Sets the position so that the sprite is centered on (x, y)

public void
setCenterX(float x)

Sets the x position so that it is centered on the given x parameter

public void
setCenterY(float y)

Sets the y position so that it is centered on the given y parameter

public void
setColor(Color tint)

Sets the color used to tint this sprite.

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

public void
setFlip(boolean
the desired horizontal flip state
x
,
boolean
the desired vertical flip state
y
)

Set the sprite's flip state regardless of current condition

public void
setOrigin(float originX, float originY)

Sets the origin in relation to the sprite's position for scaling and rotation.

public void
setOriginBasedPosition(float x, float y)

Sets the position where the sprite will be drawn, relative to its current origin.

public void
setOriginCenter()

Place origin in the center of the sprite

public void
setPackedColor(float packedColor)

Sets the packed color used to tint this sprite.

public void
setPosition(float x, float y)

Sets the position where the sprite will be drawn.

public void
setRegion(float u, float v, float u2, float v2)

Overrides com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion.

public void
setRotation(float degrees)

Sets the rotation of the sprite in degrees.

public void
setScale(float scaleXY)

Sets the sprite's scale for both X and Y uniformly.

public void
setScale(float scaleX, float scaleY)

Sets the sprite's scale for both X and Y.

public void
setSize(float width, float height)

Sets the size of the sprite when drawn, before scaling and rotation are applied.

public void
public void
public void
public void
public void
setX(float x)

Sets the x position where the sprite will be drawn.

public void
setY(float y)

Sets the y position where the sprite will be drawn.

public void
translate(float xAmount, float yAmount)

Sets the position relative to the current position where the sprite will be drawn.

public void
translateX(float xAmount)

Sets the x position relative to the current position where the sprite will be drawn.

public void
translateY(float yAmount)

Sets the y position relative to the current position where the sprite will be drawn.

Inherited from com.badlogic.gdx.graphics.g2d.TextureRegion:
getRegionHeightgetRegionWidthgetRegionXgetRegionYgetTexturegetUgetU2getVgetV2isFlipXisFlipYsetRegionsetRegionsetRegionsetRegionsetRegionHeightsetRegionWidthsetRegionXsetRegionYsetTexturesplitsplit

Field Detail

boundsback to summary
private Rectangle bounds
colorback to summary
private final Color color
dirtyback to summary
private boolean dirty
heightback to summary
pack-priv float height
originXback to summary
private float originX
originYback to summary
private float originY
packedColorback to summary
private float packedColor
rotationback to summary
private float rotation
scaleXback to summary
private float scaleX
scaleYback to summary
private float scaleY
SPRITE_SIZEback to summary
pack-priv static final int SPRITE_SIZE
VERTEX_SIZEback to summary
pack-priv static final int VERTEX_SIZE
verticesback to summary
pack-priv final float[] vertices
widthback to summary
pack-priv float width
xback to summary
private float x
yback to summary
private float y

Constructor Detail

Spriteback to summary
public Sprite()

Creates an uninitialized sprite. The sprite will need a texture region and bounds set before it can be drawn.

Spriteback to summary
public Sprite(Texture texture)

Creates a sprite with width, height, and texture region equal to the size of the texture.

Spriteback to summary
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.

Parameters
srcWidth:int

The width of the texture region. May be negative to flip the sprite when drawn.

srcHeight:int

The height of the texture region. May be negative to flip the sprite when drawn.

Spriteback to summary
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.

Parameters
srcWidth:int

The width of the texture region. May be negative to flip the sprite when drawn.

srcHeight:int

The height of the texture region. May be negative to flip the sprite when drawn.

Spriteback to summary
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

Spriteback to summary
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:int

The width of the texture region. May be negative to flip the sprite when drawn.

srcHeight:int

The height of the texture region. May be negative to flip the sprite when drawn.

Spriteback to summary
public Sprite(Sprite sprite)

Creates a sprite that is a copy in every way of the specified sprite.

Method Detail

drawback to summary
public void draw(Batch batch)
drawback to summary
public void draw(Batch batch, float alphaModulation)
flipback to summary
public void flip(boolean x, boolean y)

Overrides com.badlogic.gdx.graphics.g2d.TextureRegion.flip.

boolean parameters x,y are not setting a state, but performing a flip

Parameters
x:boolean

perform horizontal flip

y:boolean

perform vertical flip

getBoundingRectangleback to summary
public 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:Rectangle

the bounding Rectangle

getColorback to summary
public Color getColor()

Returns the color of this sprite. If the returned instance is manipulated, setColor(Color) must be called afterward.

getHeightback to summary
public float getHeight()
Returns:float

the height of the sprite, not accounting for scale.

getOriginXback to summary
public float getOriginX()

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

getOriginYback to summary
public float getOriginY()

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

getPackedColorback to summary
public float getPackedColor()

Returns the packed color of this sprite.

getRotationback to summary
public float getRotation()
Returns:float

the rotation of the sprite in degrees

getScaleXback to summary
public float getScaleX()

X scale of the sprite, independent of size set by setSize(float, float)

getScaleYback to summary
public float getScaleY()

Y scale of the sprite, independent of size set by setSize(float, float)

getVerticesback to summary
public float[] getVertices()

Returns the packed vertices, colors, and texture coordinates for this sprite.

getWidthback to summary
public float getWidth()
Returns:float

the width of the sprite, not accounting for scale.

getXback to summary
public float getX()
getYback to summary
public float getY()
rotateback to summary
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)

rotate90back to summary
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).

scaleback to summary
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()

scrollback to summary
public void scroll(float xAmount, float yAmount)

Overrides com.badlogic.gdx.graphics.g2d.TextureRegion.scroll.

Doc from com.badlogic.gdx.graphics.g2d.TextureRegion.scroll.

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.

setback to summary
public void set(Sprite sprite)

Make this sprite a copy in every way of the specified sprite

setAlphaback to summary
public void setAlpha(float a)

Sets the alpha portion of the color used to tint this sprite.

setBoundsback to summary
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.

setCenterback to summary
public void setCenter(float x, float y)

Sets the position so that the sprite is centered on (x, y)

setCenterXback to summary
public void setCenterX(float x)

Sets the x position so that it is centered on the given x parameter

setCenterYback to summary
public void setCenterY(float y)

Sets the y position so that it is centered on the given y parameter

setColorback to summary
public void setColor(Color tint)

Sets the color used to tint this sprite. Default is Color#WHITE.

setColorback to summary
public void setColor(float r, float g, float b, float a)
See Also
setColor(Color)
setFlipback to summary
public void setFlip(boolean x, boolean y)

Set the sprite's flip state regardless of current condition

Parameters
x:boolean

the desired horizontal flip state

y:boolean

the desired vertical flip state

setOriginback to summary
public void setOrigin(float originX, float originY)

Sets the origin in relation to the sprite's position for scaling and rotation.

setOriginBasedPositionback to summary
public void setOriginBasedPosition(float x, float y)

Sets the position where the sprite will be drawn, relative to its current origin.

setOriginCenterback to summary
public void setOriginCenter()

Place origin in the center of the sprite

setPackedColorback to summary
public void setPackedColor(float packedColor)

Sets the packed color used to tint this sprite.

See Also
setColor(Color), Color#toFloatBits()
setPositionback to summary
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).

setRegionback to summary
public void setRegion(float u, float v, float u2, float v2)

Overrides com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion.

setRotationback to summary
public void setRotation(float degrees)

Sets the rotation of the sprite in degrees. Rotation is centered on the origin set in setOrigin(float, float)

setScaleback to summary
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()

setScaleback to summary
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()

setSizeback to summary
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).

setUback to summary
public void setU(float u)

Overrides com.badlogic.gdx.graphics.g2d.TextureRegion.setU.

setU2back to summary
public void setU2(float u2)

Overrides com.badlogic.gdx.graphics.g2d.TextureRegion.setU2.

setVback to summary
public void setV(float v)

Overrides com.badlogic.gdx.graphics.g2d.TextureRegion.setV.

setV2back to summary
public void setV2(float v2)

Overrides com.badlogic.gdx.graphics.g2d.TextureRegion.setV2.

setXback to summary
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).

setYback to summary
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).

translateback to summary
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.

translateXback to summary
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.

translateYback to summary
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.