Skip to main content

FlixelSprite

View source

class

org.flixelgdx.FlixelSprite

public class FlixelSprite extends FlixelObject implements FlixelAntialiasable, FlixelColorable, FlixelShaderable

The core building block of all FlixelGDX games. Extends FlixelObject with graphical capabilities including texture rendering, scaling, rotation, tinting, and flipping.

Frame-based clips, Sparrow/XML atlases, and playback use a FlixelAnimationController that is not allocated by default (saves memory for large sprite counts on the order of thousands of extra sprites before overhead dominates). Call FlixelSprite.ensureAnimation() or assign a controller directly when you need clips, then use sprite.ensureAnimation().addSparrowAtlas(...), .playAnimation(...), etc.

It is common to extend FlixelSprite for your own game's needs; for example, a SpaceShip class may extend FlixelSprite but add additional game-specific fields.

Constructors

FlixelSprite()

public FlixelSprite()

Constructs a new FlixelSprite with default values.


FlixelSprite(float, float)

public FlixelSprite(float x, float y)

Constructs a new sprite at the given position.

Parameters:

NameDescription
xThe X coordinate to place the new sprite at.
yThe X coordinate to place the new sprite at.

FlixelSprite(float, float, String)

public FlixelSprite(float x, float y, String graphicAssetKey)

Constructs a new sprite at the given position with a loaded graphic.

Parameters:

NameDescription
xThe X coordinate to place the new sprite at.
yThe X coordinate to place the new sprite at.

Fields

graphic

protected FlixelGraphic graphic

Graphic backing this sprite (shared/cached wrapper around a Texture).


atlasFrames

protected Array<FlixelFrame> atlasFrames

The atlas frames used in this sprite (used for animations).


secondaryGraphics

protected Array<FlixelGraphic> secondaryGraphics

Extra FlixelGraphic handles retained when additional spritesheets are merged onto this sprite, so those atlases' textures stay loaded for the sprite's lifetime. The primary graphic is still tracked by the FlixelSprite.graphic field; this list only holds graphics appended after the initial load, for example through FlixelSprite.mergeSparrowAtlas(FlixelGraphic, Array<FlixelFrame>) or an appended Animate rig atlas. Lazily allocated to avoid the per-instance footprint for sprites that only ever load one atlas.


animation

public FlixelAnimationController animation

Heavy controller object for handling animations. null until FlixelSprite.ensureAnimation() or assigned directly.


currentFrame

protected FlixelFrame currentFrame

The current frame that this sprite is currently using for drawing.


frames

protected FlixelFrame[][] frames

Where all the image frames are stored. This is also where the main image is stored when using FlixelSprite.loadGraphic(FileHandle).


scaleX

protected float scaleX

Horizontal scale factor. 1 = normal size.


scaleY

protected float scaleY

Vertical scale factor. 1 = normal size.


originX

protected float originX

X component of the rotation/scale origin point.


originY

protected float originY

Y component of the rotation/scale origin point.


offsetX

protected float offsetX

The offset from the sprite's position to its graphic.


offsetY

protected float offsetY

The offset from the sprite's position to its graphic.


color

protected final Color color

The color tint applied when drawing this sprite.


facing

protected int facing

The direction this sprite is facing. Useful for automatic flipping.


antialiasing

protected boolean antialiasing

Whether this sprite is smoothed when scaled.


flipX

protected boolean flipX

Whether this sprite is flipped horizontally.


flipY

protected boolean flipY

Whether this sprite is flipped vertically.


Methods

ensureAnimation()

public FlixelAnimationController ensureAnimation()

Returns the existing controller or creates and assigns a new FlixelAnimationController for this sprite.


update(float)

public void update(float elapsed)

Updates this sprite.

Parameters:

NameDescription
elapsedThe amount of time that has passed since the last frame update.

setCurrentFrameForAnimation(FlixelFrame)

public void setCurrentFrameForAnimation(FlixelFrame frame)

Called by FlixelAnimationController when the displayed keyframe changes.

Parameters:

NameDescription
frameThe frame to draw, or null to leave static graphic unchanged.

clearAnimationDisplayFrame()

public void clearAnimationDisplayFrame()

Clears the active Sparrow / atlas / animation display frame. FlixelSprite.draw(FlixelBatch) will draw nothing until a frame is set again (e.g. by FlixelAnimationController or FlixelSprite.applySparrowAtlas(FlixelGraphic, Array<FlixelFrame>)).


loadGraphic(FileHandle)

public FlixelSprite loadGraphic(FileHandle path)

Load's a texture and automatically resizes the size of this sprite.

Parameters:

NameDescription
pathThe directory of the .png to load onto this sprite.

Returns: this sprite for chaining.


loadGraphic(FileHandle, int)

public FlixelSprite loadGraphic(FileHandle path, int frameWidth)

Load's a texture and automatically resizes the size of this sprite.

Parameters:

NameDescription
pathThe directory of the .png to load onto this sprite.
frameWidthHow wide the sprite should be.

Returns: this sprite for chaining.


loadGraphic(FileHandle, int, int)

public FlixelSprite loadGraphic(FileHandle path, int frameWidth, int frameHeight)

Load's a texture and automatically resizes the size of this sprite.

Parameters:

NameDescription
pathThe directory of the .png to load onto this sprite.
frameWidthHow wide the sprite should be.
frameHeightHow tall the sprite should be.

Returns: this sprite for chaining.


loadGraphic(Texture, int, int)

public FlixelSprite loadGraphic(Texture texture, int frameWidth, int frameHeight)

Loads a texture and automatically resizes the size of this sprite.

Parameters:

NameDescription
textureThe texture to load onto this sprite (owned by caller).
frameWidthHow wide the sprite should be.
frameHeightHow tall the sprite should be.

Returns: this sprite for chaining.


loadGraphic(String)

public FlixelSprite loadGraphic(String assetKey)

Loads a cached graphic by key. Queue the asset with FlixelAssetManager.load(String) in a loading state to avoid synchronous stalls on the first frame.

Parameters:

NameDescription
assetKeyThe key of the graphic to load.

Returns: this sprite for chaining.


loadGraphic(String, int)

public FlixelSprite loadGraphic(String assetKey, int frameWidth)

Loads a cached graphic by key. Queue the asset with FlixelAssetManager.load(String) in a loading state to avoid synchronous stalls on the first frame.

Parameters:

NameDescription
assetKeyThe key of the graphic to load.
frameWidthThe width of the graphic.

Returns: this sprite for chaining.


loadGraphic(String, int, int)

public FlixelSprite loadGraphic(String assetKey, int frameWidth, int frameHeight)

Loads a cached graphic by key. Queue the asset with FlixelAssetManager.load(String) in a loading state to avoid synchronous stalls on the first frame.

Parameters:

NameDescription
assetKeyThe key of the graphic to load.
frameWidthThe width of the graphic.
frameHeightThe height of the graphic.

Returns: this sprite for chaining.


loadGraphic(FlixelGraphic)

public FlixelSprite loadGraphic(FlixelGraphic g)

Loads a graphic from a FlixelGraphic.

Parameters:

NameDescription
gThe FlixelGraphic to load.

Returns: this sprite for chaining.


loadGraphic(FlixelGraphic, int)

public FlixelSprite loadGraphic(FlixelGraphic g, int frameWidth)

Loads a graphic from a FlixelGraphic.

Parameters:

NameDescription
gThe FlixelGraphic to load.
frameWidthThe width of the graphic.

Returns: this sprite for chaining.


loadGraphic(FlixelGraphic, int, int)

public FlixelSprite loadGraphic(FlixelGraphic g, int frameWidth, int frameHeight)

Loads a graphic from a FlixelGraphic.

Parameters:

NameDescription
gThe FlixelGraphic to load.
frameWidthThe width of the graphic.
frameHeightThe height of the graphic.

Returns: this sprite for chaining.


makeGraphic(int, int, Color)

public FlixelSprite makeGraphic(int width, int height, Color color)

Creates a solid color rectangular texture on the fly.

Parameters:

NameDescription
widthThe width of the graphic.
heightThe height of the graphic.
colorThe color of the graphic.

Returns: this sprite for chaining.


makeGraphic(int, int, FlixelColor)

public FlixelSprite makeGraphic(int width, int height, FlixelColor color)

Creates a solid color rectangular texture on the fly.

Parameters:

NameDescription
widthThe width of the graphic.
heightThe height of the graphic.
colorThe color of the graphic.

Returns: this sprite for chaining.


applySparrowAtlas(FlixelGraphic, Array<FlixelFrame>)

public void applySparrowAtlas(FlixelGraphic newGraphic, Array<FlixelFrame> parsedFrames)

Installs a retained FlixelGraphic and parsed Sparrow atlas frames. Called by FlixelAnimationController.addSparrowFrames(String) and FlixelSpritemapJsonLoader.load(...), not a general API for game code.

Parameters:

NameDescription
newGraphicGraphic from Flixel.ensureAssets().get(...) with retain() already called.
parsedFramesFrames built from the XML (which may be empty).

mergeSparrowAtlas(FlixelGraphic, Array<FlixelFrame>)

public void mergeSparrowAtlas(FlixelGraphic newGraphic, Array<FlixelFrame> parsedFrames)

Merges parsed Sparrow atlas frames onto this sprite's existing atlas instead of replacing it.

Where FlixelSprite.applySparrowAtlas(FlixelGraphic, Array<FlixelFrame>) swaps in a fresh atlas and clears the sprite's clips, this appends parsedFrames to whatever atlas the sprite already has (creating one when it had none) and retains newGraphic as a secondary graphic so its texture stays loaded. That lets a single sprite carry frames from more than one sheet, which is what FlixelAnimationController.addSparrowFrames(String) builds on. The currently displayed frame and the registered clips are left untouched, so a sprite already showing a rig clip or another atlas keeps rendering exactly as before; play one of the newly registered clips to show the merged art.

Parameters:

NameDescription
newGraphicThe graphic backing parsedFrames, already retained by its loader.
parsedFramesThe frames to append, which may be empty.

retainSecondaryGraphic(FlixelGraphic)

public void retainSecondaryGraphic(FlixelGraphic graphic)

Retains an additional FlixelGraphic so its texture stays loaded until this sprite is destroyed, and propagates the sprite's current antialiasing setting onto the new graphic's texture so an appended atlas matches the visual filter of the original.

The graphic is assumed to have already been retained by the caller (typically via FlixelAssetManager.get(...) followed by retain()), so this method only stores the reference and does not call FlixelGraphic.retain() again. This is an advanced hook used by atlas-merging code such as FlixelAnimationController.addSparrowFrames(String) and the Animate rig loader; most game code never calls it directly.

Parameters:

NameDescription
graphicThe graphic to retain for the sprite's lifetime. Must not be null.

draw(FlixelBatch)

public void draw(FlixelBatch batch)

setGraphicSize(int, int)

public FlixelSprite setGraphicSize(int width, int height)

Sets how large the graphic is drawn on screen (in pixels), without changing which part of the texture is used.

This adjusts FlixelSprite.setScale(float, float) so the full current frame/region maps to the given size. It does not change TextureRegion bounds: TextureRegion#setRegionWidth/setRegionHeight only resize the source rectangle inside the texture (UVs), which crops or re-samples texels; the drawable size in this class comes from FlixelSprite.getWidth()/FlixelSprite.getHeight() and scale in FlixelSprite.draw(FlixelBatch).

Parameters:

NameDescription
widthThe drawn width in pixels (must be > 0).
heightThe drawn height in pixels (must be > 0).

Returns: this sprite for chaining.


updateHitbox()

public FlixelSprite updateHitbox()

Sets the hitbox to match the on-screen graphic.

FlixelSprite.draw(FlixelBatch) sizes every frame from FlixelFrame.originalWidth/FlixelFrame.originalHeight and scale separately, so the hitbox is set to the frame's untrimmed source size times |scale| so the box frames the whole drawn artwork (matching HaxeFlixel's frameWidth/frameHeight), not just the trimmed pixels.


updateHitbox(float, float)

public FlixelSprite updateHitbox(float width, float height)

Updates the hitbox of this sprite to the size of the given width and height.

Parameters:

NameDescription
widthThe width of the hitbox.
heightThe height of the hitbox.

Returns: this sprite for chaining.


screenCenter()

public FlixelSprite screenCenter()

Centers this sprite on the screen.

Returns: this sprite for chaining.


screenCenter(FlixelAxes)

public FlixelSprite screenCenter(FlixelAxes axes)

Centers this sprite on the screen.

Parameters:

NameDescription
axesThe axes to center on.

Returns: this sprite for chaining.


destroy()

public void destroy()

setShader(FlixelShader)

public void setShader(FlixelShader shader)

Assigns a shader that is applied to this sprite individually when it is drawn.

Each unique shader transition in draw order flushes the GPU vertex buffer before the new shader takes over. Consecutive sprites that share the same FlixelShader instance batch together for free. If you mix many different shaders across sprites in a single camera, performance may drop noticeably on weak devices. Giving players the option to disable sprite shaders is strongly recommended.

The shader is NOT owned by this sprite. Call FlixelShader.destroy() yourself when the shader is no longer needed. Pass null to remove the current shader.

If you need a full-scene effect (post-processing applied to everything a camera sees), prefer FlixelCamera.setShader() instead, as it captures the entire scene into a single FBO and applies the shader once, with no per-sprite flush cost.

Parameters:

NameDescription
shaderThe shader to apply when drawing this sprite, or null to remove it.

getShader()

public FlixelShader getShader()

Returns the shader currently assigned to this sprite, or null if none is set.

Returns: The active per-sprite FlixelShader, or null.


hasOwnedGraphic()

public boolean hasOwnedGraphic()

Whether this sprite holds an owned FlixelGraphic (e.g. from FlixelSprite.makeGraphic(...)), so CPU-side pixmap uploads are allowed without mutating a shared atlas.


getGraphic()

public FlixelGraphic getGraphic()

getTexture()

public Texture getTexture()

getScaleX()

public float getScaleX()

getScaleY()

public float getScaleY()

setScale(float)

public void setScale(float scaleXY)

setScale(float, float)

public void setScale(float scaleX, float scaleY)

setScaleX(float)

public void setScaleX(float scaleX)

setScaleY(float)

public void setScaleY(float scaleY)

getOriginX()

public float getOriginX()

getOriginY()

public float getOriginY()

setOrigin(float, float)

public void setOrigin(float originX, float originY)

setOriginCenter()

public void setOriginCenter()

getOffsetX()

public float getOffsetX()

setOffsetX(float)

public void setOffsetX(float offsetX)

getOffsetY()

public float getOffsetY()

setOffsetY(float)

public void setOffsetY(float offsetY)

setOffset(float, float)

public void setOffset(float x, float y)

isAntialiasing()

public boolean isAntialiasing()

getAntialiasing()

public boolean getAntialiasing()

Returns whether linear texture filtering (antialiasing) is enabled for this sprite.


setAntialiasing(boolean)

public void setAntialiasing(boolean antialiasing)

toggleAntialiasing()

public void toggleAntialiasing()

getAlpha()

public float getAlpha()

getFacing()

public int getFacing()

setFacing(int)

public void setFacing(int facing)

getBlendMode()

public FlixelBlendMode getBlendMode()

setBlendMode(FlixelBlendMode)

public void setBlendMode(FlixelBlendMode blendMode)

getColor()

public int getColor()

getGdxColor()

public Color getGdxColor()

setColor(Color)

public void setColor(Color tint)

Copies RGBA from color into this tint.


setColor(FlixelColor)

public void setColor(FlixelColor tint)

Copies RGBA from color into this tint.


setColor(float, float, float, float)

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

getShakeX()

public float getShakeX()

Current shake channel X (for example graphic offset or world X).


getShakeY()

public float getShakeY()

Current shake channel Y.


setShake(float, float)

public void setShake(float x, float y)

Sets the shake channel to absolute coordinates (typically base plus jitter).


setAlpha(float)

public void setAlpha(float a)

flip(boolean, boolean)

public void flip(boolean x, boolean y)

isFlipX()

public boolean isFlipX()

getFlipX()

public boolean getFlipX()

setFlipX(boolean)

public void setFlipX(boolean flipX)

isFlipY()

public boolean isFlipY()

getFlipY()

public boolean getFlipY()

setFlipY(boolean)

public void setFlipY(boolean flipY)

setRegion(TextureRegion)

public void setRegion(TextureRegion region)

getFrame()

public FlixelFrame getFrame()

getRegion()

public TextureRegion getRegion()

getRegionWidth()

public int getRegionWidth()

getRegionHeight()

public int getRegionHeight()

getAtlasRegions()

public Array<FlixelFrame> getAtlasRegions()

getCurrentFrame()

public FlixelFrame getCurrentFrame()

getFrames()

public FlixelFrame[][] getFrames()

isClipRectEnabled()

public boolean isClipRectEnabled()

setClipRect(float, float, float, float)

public void setClipRect(float x, float y, float width, float height)

Sets the clip rectangle in screen-pixel space relative to the sprite's drawn position, and enables clipping.

Only the region inside the rectangle is drawn; pixels outside are discarded by the GPU scissor. Coordinates are in the same units as FlixelSprite.getWidth()/FlixelSprite.getHeight() - that is, they already account for scale, so x=0, y=0 anchors to the drawn bottom-left corner and width=getWidth() covers the full drawn width regardless of scale.

For example, to show only the left half of a sprite regardless of its current scale:

sprite.setClipRect(0, 0, sprite.getWidth() * 0.5f, sprite.getHeight());
// Slide the window right by 10 px later:
sprite.changeClipRectX(10);
// Remove clipping:
sprite.clearClipRect();

Parameters:

NameDescription
xLeft edge of the visible region, in screen pixels from the sprite's drawn left edge.
yBottom edge of the visible region, in screen pixels from the sprite's drawn bottom edge.
widthWidth of the visible region, in screen pixels.
heightHeight of the visible region, in screen pixels.

clearClipRect()

public void clearClipRect()

Disables the clip rectangle and resets all clip values to zero.


getClipRectX()

public float getClipRectX()

setClipRectX(float)

public void setClipRectX(float clipRectX)

changeClipRectX(float)

public void changeClipRectX(float clipRectX)

getClipRectY()

public float getClipRectY()

setClipRectY(float)

public void setClipRectY(float clipRectY)

changeClipRectY(float)

public void changeClipRectY(float clipRectY)

getClipRectWidth()

public float getClipRectWidth()

setClipRectWidth(float)

public void setClipRectWidth(float clipRectWidth)

changeClipRectWidth(float)

public void changeClipRectWidth(float clipRectWidth)

getClipRectHeight()

public float getClipRectHeight()

setClipRectHeight(float)

public void setClipRectHeight(float clipRectHeight)

changeClipRectHeight(float)

public void changeClipRectHeight(float clipRectHeight)