Skip to main content

FlixelSpriteUtil

View source

class

org.flixelgdx.util.FlixelSpriteUtil

public final class FlixelSpriteUtil

Helper class related to FlixelSprite.

These utilities are designed to work with FlixelGDX's normal Batch-based draw flow. Avoid using ShapeRenderer in core game rendering unless you control the render pipeline.

Do not call FlixelSpriteUtil.createWhitePixelTexture() every frame or per sprite instance in hot paths. That allocates new Pixmap and Texture objects and will spike heap usage on most JVMs. Prefer FlixelSpriteUtil.obtainWhitePixelTexture(FlixelAssetManager), which registers a single persistent texture with the asset manager (see FlixelDefaultAssetManager) and reuses it for the lifetime of the game.

FlixelSpriteUtil.fill(FlixelSprite, Color) writes solid pixels into the active frame when possible, and FlixelSpriteUtil.setBrightness(FlixelSprite, float) adjusts tint like Animate brightness.

Fields

WHITE_PIXEL_TEXTURE_KEY

public static final String WHITE_PIXEL_TEXTURE_KEY = "__flixel_internal__/white_pixel_1x1"

Fixed asset key for the framework-owned 1x1 white Texture registered via FlixelSpriteUtil.obtainWhitePixelTexture(FlixelAssetManager).


Methods

obtainWhitePixelTexture(FlixelAssetManager)

public static Texture obtainWhitePixelTexture(FlixelAssetManager assets)

Returns the shared 1x1 white Texture registered with assets. The first call creates the texture, wraps it in a FlixelGraphic with FlixelGraphic.setPersist(boolean) true, and registers it with FlixelAssetManager.register(FlixelAsset<?>). Callers must not Texture.dispose() this texture; lifecycle follows the asset manager.

Parameters:

NameDescription
assetsNon-null manager from Flixel.ensureAssets().

createWhitePixelTexture()

public static Texture createWhitePixelTexture()

Creates a 1x1 white pixel texture.

Ownership: Prefer FlixelSpriteUtil.obtainWhitePixelTexture(FlixelAssetManager) so only one instance exists. If you call this directly, you own the returned texture and must dispose it.

Returns: The created white pixel texture.


drawRect(Batch, Texture, float, float, float, float, Color)

public static void drawRect(Batch batch, Texture whitePixel, float x, float y, float w, float h, Color color)

Draws a rectangle with the given color.

Parameters:

NameDescription
batchThe batch to draw the rectangle on.
whitePixelThe white pixel texture to use for the rectangle.
xThe x position of the rectangle.
yThe y position of the rectangle.
wThe width of the rectangle.
hThe height of the rectangle.
colorThe color of the rectangle.

drawBorder(Batch, Texture, float, float, float, float, float, Color)

public static void drawBorder(Batch batch, Texture whitePixel, float x, float y, float w, float h, float thickness, Color color)

Draws a border around the given rectangle.

Parameters:

NameDescription
batchThe batch to draw the border on.
whitePixelThe white pixel texture to use for the border.
xThe x position of the rectangle.
yThe y position of the rectangle.
wThe width of the rectangle.
hThe height of the rectangle.
thicknessThe thickness of the border.
colorThe color of the border.

createLinearGradientPixmap(int, int, Color, Color, boolean)

public static Pixmap createLinearGradientPixmap(int width, int height, Color start, Color end, boolean horizontal)

Creates a 2-color linear gradient Pixmap.

Caller owns the returned Pixmap and must dispose of it through Pixmap.dispose() themselves.

Parameters:

NameDescription
widthThe width of the gradient.
heightThe height of the gradient.
startThe start color of the gradient.
endThe end color of the gradient.
horizontalWhether the gradient is horizontal.

Returns: The created gradient Pixmap.


createLinearGradientTexture(int, int, Color, Color, boolean)

public static Texture createLinearGradientTexture(int width, int height, Color start, Color end, boolean horizontal)

Creates a 2-color linear gradient Texture.

Caller owns the returned Texture and must dispose of it through Texture.dispose() themselves.

Parameters:

NameDescription
widthThe width of the gradient.
heightThe height of the gradient.
startThe start color of the gradient.
endThe end color of the gradient.
horizontalWhether the gradient is horizontal.

Returns: The created gradient Texture.


bound(FlixelSprite, float, float, float, float)

public static FlixelSprite bound(FlixelSprite sprite, float minX, float maxX, float minY, float maxY)

Clamps the sprite inside a world rectangle. When maxX or maxY are 0 or less, the game view size from Flixel.getWidth() / Flixel.getHeight() is used for that axis.

Parameters:

NameDescription
spriteThe sprite to bound.
minXThe minimum x position.
maxXThe maximum x position.
minYThe minimum y position.
maxYThe maximum y position.

Returns: The bounded sprite.


screenWrap(FlixelSprite, boolean, boolean, boolean, boolean)

public static FlixelSprite screenWrap(FlixelSprite sprite, boolean left, boolean right, boolean top, boolean bottom)

Wraps the sprite when it leaves the game view (same default max as FlixelSpriteUtil.bound(...) when zeros).

Parameters:

NameDescription
spriteThe sprite to wrap.
leftWhether to wrap the sprite when it leaves the left edge of the game view.
rightWhether to wrap the sprite when it leaves the right edge of the game view.
topWhether to wrap the sprite when it leaves the top edge of the game view.
bottomWhether to wrap the sprite when it leaves the bottom edge of the game view.

Returns: The wrapped sprite.


cameraBound(FlixelSprite, FlixelCamera, boolean, boolean, boolean, boolean)

public static FlixelSprite cameraBound(FlixelSprite sprite, FlixelCamera camera, boolean left, boolean right, boolean top, boolean bottom)

Keeps the sprite inside the given camera world view (FlixelCamera.getViewLeft() etc.).

Parameters:

NameDescription
spriteThe sprite to bound.
cameraThe camera to bound the sprite to.
leftWhether to bound the sprite when it leaves the left edge of the camera view.
rightWhether to bound the sprite when it leaves the right edge of the camera view.
topWhether to bound the sprite when it leaves the top edge of the camera view.
bottomWhether to bound the sprite when it leaves the bottom edge of the camera view.

Returns: The bounded sprite.


cameraWrap(FlixelSprite, FlixelCamera, boolean, boolean, boolean, boolean)

public static FlixelSprite cameraWrap(FlixelSprite sprite, FlixelCamera camera, boolean left, boolean right, boolean top, boolean bottom)

Wraps the sprite relative to the camera view edges.

Parameters:

NameDescription
spriteThe sprite to wrap.
cameraThe camera to wrap the sprite relative to.
leftWhether to wrap the sprite when it leaves the left edge of the camera view.
rightWhether to wrap the sprite when it leaves the right edge of the camera view.
topWhether to wrap the sprite when it leaves the top edge of the camera view.
bottomWhether to wrap the sprite when it leaves the bottom edge of the camera view.

Returns: The wrapped sprite.


fill(FlixelSprite, Color)

public static FlixelSprite fill(FlixelSprite sprite, Color fillColor)

Fills the active bitmap area with a solid color, similar to HaxeFlixel FlxSpriteUtil.fill.

When the sprite uses an owned pixmap-backed Texture (FlixelSprite.hasOwnedGraphic()), every pixel in the current TextureRegion (animation frame or static region) is overwritten via Texture.draw(...) without replacing the graphic.

Otherwise (shared atlas, unloaded graphic, or non-pixmap GPU texture) this falls back to FlixelSprite.makeGraphic(...) sized to the logical frame (FlixelFrame.originalWidth / FlixelFrame.originalHeight when set, else region size, else object size).

Parameters:

NameDescription
spriteThe sprite to fill.
fillColorThe color to fill with (including alpha).

Returns: sprite for chaining.


setBrightness(FlixelSprite, float)

public static void setBrightness(FlixelSprite sprite, float brightness)

Adjusts RGB toward white (brightness = 1) or black (brightness = -1), matching Adobe Animate style brightness as described for FlixelGDX FlixelSpriteUtil.setBrightness. Alpha is preserved.

Parameters:

NameDescription
spriteThe sprite to set the brightness of.
brightnessClamped to [-1, 1].

drawLine(Batch, Texture, float, float, float, float, float, Color)

public static void drawLine(Batch batch, Texture whitePixel, float startX, float startY, float endX, float endY, float thickness, Color color)

Draws a line segment using a 1x1 texture and Batch. Does not allocate.

Parameters:

NameDescription
batchThe batch to draw the line on.
whitePixelThe white pixel texture to use for the line.
startXThe x position of the start of the line.
startYThe y position of the start of the line.
endXThe x position of the end of the line.
endYThe y position of the end of the line.
thicknessThe thickness of the line.
colorThe color of the line.

getMidpoint(FlixelSprite, Vector2)

public static Vector2 getMidpoint(FlixelSprite sprite, Vector2 out)

Midpoint of the given sprite's hitbox in world space. Reuses out when non-null.

Parameters:

NameDescription
spriteThe sprite to get the midpoint of.
outThe vector to store the midpoint in.

Returns: The midpoint of the sprite.