Class ParentSprite
java.lang.Object
com.badlogic.gdx.graphics.g2d.TextureRegion
com.github.yellowstonegames.smooth.ParentSprite
- Direct Known Subclasses:
AnimatedGlidingSprite, GlidingSprite
public class ParentSprite
extends com.badlogic.gdx.graphics.g2d.TextureRegion
Holds the geometry, color, and texture information for drawing 2D sprites using
A ParentSprite has a position and a size given as width and height. The position is relative to the origin of the coordinate system specified via
Classes that extend this have full access to fields in Sprite that were private or package-private. However, they will not have access to the package-private fields in the parent TextureRegion class, so you'll need to use getters and setters for those. If you're copying code from Sprite, it's usually a good idea to statically import constants from SpriteBatch:
Batch. This is exactly the
same as Sprite except that it is meant to be extended, and so it has no private
or package-private variables or methods. You're welcome.
A ParentSprite 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 ParentSprite is always
rectangular and its position (x, y) are located in the bottom left corner of that rectangle. A ParentSprite 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 ParentSprite, its position.
Classes that extend this have full access to fields in Sprite that were private or package-private. However, they will not have access to the package-private fields in the parent TextureRegion class, so you'll need to use getters and setters for those. If you're copying code from Sprite, it's usually a good idea to statically import constants from SpriteBatch:
import static com.badlogic.gdx.graphics.g2d.SpriteBatch.*;-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected com.badlogic.gdx.math.Rectangleprotected final com.badlogic.gdx.graphics.Colorprotected booleanfloatprotected floatprotected floatprotected floatprotected floatprotected floatstatic final intstatic final intprotected final float[]floatprotected floatprotected float -
Constructor Summary
ConstructorsConstructorDescriptionCreates an uninitialized sprite.ParentSprite(com.badlogic.gdx.graphics.g2d.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 otherParentSprite(com.badlogic.gdx.graphics.g2d.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.ParentSprite(com.badlogic.gdx.graphics.Texture texture) Creates a sprite with width, height, and texture region equal to the size of the texture.ParentSprite(com.badlogic.gdx.graphics.Texture texture, int srcWidth, int srcHeight) Creates a sprite with width, height, and texture region equal to the specified size.ParentSprite(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.ParentSprite(ParentSprite sprite) Creates a sprite that is a copy in every way of the specified sprite. -
Method Summary
Modifier and TypeMethodDescriptionvoiddraw(com.badlogic.gdx.graphics.g2d.Batch batch) voiddraw(com.badlogic.gdx.graphics.g2d.Batch batch, float alphaModulation) voidflip(boolean x, boolean y) boolean parameters x,y are not setting a state, but performing a flipcom.badlogic.gdx.math.RectangleReturns the bounding axis alignedRectanglethat bounds this sprite.com.badlogic.gdx.graphics.ColorgetColor()Returns the color of this sprite.floatfloatThe origin influencessetPosition(float, float),setRotation(float)and the expansion direction of scalingsetScale(float, float)floatThe origin influencessetPosition(float, float),setRotation(float)and the expansion direction of scalingsetScale(float, float)floatfloatX scale of the sprite, independent of size set bysetSize(float, float)floatY scale of the sprite, independent of size set bysetSize(float, float)float[]Returns the packed vertices, colors, and texture coordinates for this sprite.floatgetWidth()floatgetX()floatgetY()voidrotate(float degrees) Sets the sprite's rotation in degrees relative to the current rotation.voidrotate90(boolean clockwise) Rotates this sprite 90 degrees in-place by rotating the texture coordinates.voidscale(float amount) Sets the sprite's scale relative to the current scale.voidscroll(float xAmount, float yAmount) voidset(ParentSprite sprite) Make this sprite a copy in every way of the specified spritevoidsetAlpha(float a) Sets the alpha portion of the color used to tint this sprite.voidsetBounds(float x, float y, float width, float height) Sets the position and size of the sprite when drawn, before scaling and rotation are applied.voidsetCenter(float x, float y) Sets the position so that the sprite is centered on (x, y)voidsetCenterX(float x) Sets the x position so that it is centered on the given x parametervoidsetCenterY(float y) Sets the y position so that it is centered on the given y parametervoidsetColor(float r, float g, float b, float a) voidsetColor(com.badlogic.gdx.graphics.Color tint) Sets the color used to tint this sprite.voidsetFlip(boolean x, boolean y) Set the sprite's flip state regardless of current conditionvoidsetOrigin(float originX, float originY) Sets the origin in relation to the sprite's position for scaling and rotation.voidsetOriginBasedPosition(float x, float y) Sets the position where the sprite will be drawn, relative to its current origin.voidPlace origin in the center of the spritevoidsetPackedColor(float packedColor) Sets the color of this sprite, expanding the alpha from 0-254 to 0-255.voidsetPosition(float x, float y) Sets the position where the sprite will be drawn.voidsetRegion(float u, float v, float u2, float v2) voidsetRotation(float degrees) Sets the rotation of the sprite in degrees.voidsetScale(float scaleXY) Sets the sprite's scale for both X and Y uniformly.voidsetScale(float scaleX, float scaleY) Sets the sprite's scale for both X and Y.voidsetSize(float width, float height) Sets the size of the sprite when drawn, before scaling and rotation are applied.voidsetU(float u) voidsetU2(float u2) voidsetV(float v) voidsetV2(float v2) voidsetX(float x) Sets the x position where the sprite will be drawn.voidsetY(float y) Sets the y position where the sprite will be drawn.voidtranslate(float xAmount, float yAmount) Sets the position relative to the current position where the sprite will be drawn.voidtranslateX(float xAmount) Sets the x position relative to the current position where the sprite will be drawn.voidtranslateY(float yAmount) Sets the y 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, isFlipX, isFlipY, setRegion, setRegion, setRegion, setRegion, setRegionHeight, setRegionWidth, setRegionX, setRegionY, setTexture, split, split
-
Field Details
-
VERTEX_SIZE
public static final int VERTEX_SIZE- See Also:
-
SPRITE_SIZE
public static final int SPRITE_SIZE- See Also:
-
vertices
protected final float[] vertices -
color
protected final com.badlogic.gdx.graphics.Color color -
x
protected float x -
y
protected float y -
width
public float width -
height
public float height -
originX
protected float originX -
originY
protected float originY -
rotation
protected float rotation -
scaleX
protected float scaleX -
scaleY
protected float scaleY -
dirty
protected boolean dirty -
bounds
protected com.badlogic.gdx.math.Rectangle bounds
-
-
Constructor Details
-
ParentSprite
public ParentSprite()Creates an uninitialized sprite. The sprite will need a texture region and bounds set before it can be drawn. -
ParentSprite
public ParentSprite(com.badlogic.gdx.graphics.Texture texture) Creates a sprite with width, height, and texture region equal to the size of the texture. -
ParentSprite
public ParentSprite(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.
-
ParentSprite
public ParentSprite(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.
-
ParentSprite
public ParentSprite(com.badlogic.gdx.graphics.g2d.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 -
ParentSprite
public ParentSprite(com.badlogic.gdx.graphics.g2d.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.
-
ParentSprite
Creates a sprite that is a copy in every way of the specified sprite.
-
-
Method Details
-
set
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 usesetBounds(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 usesetBounds(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. -
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 usesetBounds(float, float, float, float). -
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 usesetBounds(float, float, float, float). -
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. -
setColor
public void setColor(com.badlogic.gdx.graphics.Color tint) Sets the color used to tint this sprite. Default isColor.WHITE. -
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 -
setRotation
public void setRotation(float degrees) Sets the rotation of the sprite in degrees. Rotation is centered on the origin set insetOrigin(float, float) -
getRotation
public float getRotation()- Returns:
- the rotation of the sprite in degrees
-
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 insetOrigin(float, float) -
rotate90
public void rotate90(boolean clockwise) Rotates this sprite 90 degrees in-place by rotating the texture coordinates. This rotation is unaffected bysetRotation(float)androtate(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 bygetWidth()andgetHeight() -
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 bygetWidth()andgetHeight() -
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 bygetWidth()andgetHeight() -
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 alignedRectanglethat 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() -
getY
public float getY() -
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 influencessetPosition(float, float),setRotation(float)and the expansion direction of scalingsetScale(float, float) -
getOriginY
public float getOriginY()The origin influencessetPosition(float, float),setRotation(float)and the expansion direction of scalingsetScale(float, float) -
getScaleX
public float getScaleX()X scale of the sprite, independent of size set bysetSize(float, float) -
getScaleY
public float getScaleY()Y scale of the sprite, independent of size set bysetSize(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. -
setRegion
public void setRegion(float u, float v, float u2, float v2) - Overrides:
setRegionin classcom.badlogic.gdx.graphics.g2d.TextureRegion
-
setU
public void setU(float u) - Overrides:
setUin classcom.badlogic.gdx.graphics.g2d.TextureRegion
-
setV
public void setV(float v) - Overrides:
setVin classcom.badlogic.gdx.graphics.g2d.TextureRegion
-
setU2
public void setU2(float u2) - Overrides:
setU2in classcom.badlogic.gdx.graphics.g2d.TextureRegion
-
setV2
public void setV2(float v2) - Overrides:
setV2in classcom.badlogic.gdx.graphics.g2d.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 statey- 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:
flipin classcom.badlogic.gdx.graphics.g2d.TextureRegion- Parameters:
x- perform horizontal flipy- perform vertical flip
-
scroll
public void scroll(float xAmount, float yAmount) - Overrides:
scrollin classcom.badlogic.gdx.graphics.g2d.TextureRegion
-