Some handy functions for FlxSprite (FlxObject) manipulation, mostly drawing-related. Note that stage quality impacts the results of the draw() functions - use FlxG.stage.quality = openfl.display.StageQuality.BEST; for best results.

Static variables

@:value(flashGfxSprite.graphics)staticread onlyflashGfx:Graphics = flashGfxSprite.graphics

@:value(new Sprite())staticread onlyflashGfxSprite:Sprite = new Sprite()

Useful helper objects for doing Flash-specific rendering. Primarily used for "debug visuals" like drawing bounding boxes directly to the screen buffer.

Static methods

staticalphaMask(output:FlxSprite, source:FlxGraphicSource, mask:FlxGraphicSource):FlxSprite

Takes two source images (typically from Embedded bitmaps) and puts the resulting image into the output FlxSprite. Note: It assumes the source and mask are the same size. Different sizes may result in undesired results. It works by copying the source image (your picture) into the output sprite. Then it removes all areas of it that do not have an alpha color value in the mask image. So if you draw a big black circle in your mask with a transparent edge, you'll get a circular image to appear. May lead to unexpected results if source does not have an alpha channel.

Parameters:

output

The FlxSprite you wish the resulting image to be placed in (will adjust width/height of image)

source

The source image. Typically the one with the image / picture / texture in it.

mask

The mask to apply. Remember the non-alpha zero areas are the parts that will display.

Returns:

The FlxSprite for chaining

staticalphaMaskFlxSprite(sprite:FlxSprite, mask:FlxSprite, output:FlxSprite):FlxSprite

Takes the image data from two FlxSprites and puts the resulting image into the output FlxSprite. Note: It assumes the source and mask are the same size. Different sizes may result in undesired results. It works by copying the source image (your picture) into the output sprite. Then it removes all areas of it that do not have an alpha color value in the mask image. So if you draw a big black circle in your mask with a transparent edge, you'll get a circular image appear. May lead to unexpected results if sprite's graphic does not have an alpha channel.

Parameters:

sprite

The source FlxSprite. Typically the one with the image / picture / texture in it.

mask

The FlxSprite containing the mask to apply. Remember the non-alpha zero areas are the parts that will display.

output

The FlxSprite you wish the resulting image to be placed in (will adjust width/height of image)

Returns:

The output FlxSprite for chaining

@:noUsingstaticinlinebeginDraw(FillColor:FlxColor, ?lineStyle:Null<LineStyle>):Void

Helper function that the drawing functions use at the start to set the color and lineStyle.

Parameters:

FillColor

The ARGB color to use for drawing

lineStyle

A LineStyle typedef containing the params of Graphics.lineStyle()

@:value({ MaxY : 0, MinY : 0, MaxX : 0, MinX : 0 })staticbound(sprite:FlxSprite, MinX:Float = 0, MaxX:Float = 0, MinY:Float = 0, MaxY:Float = 0):FlxSprite

Makes sure a FlxSprite doesn't leave the specified area - most common use case is to call this every frame in update(). If you call this without specifying an area, the game area (FlxG.width / height as max) will be used. Takes the graphic size into account.

Parameters:

sprite

The FlxSprite to bound to an area

MinX

The minimum x position allowed

MaxX

The maximum x position allowed

MinY

The minimum y position allowed

MaxY

The minimum y position allowed

Returns:

The FlxSprite for chaining

@:value({ edges : ANY })staticcameraBound(sprite:FlxSprite, ?camera:FlxCamera, edges:FlxDirectionFlags = ANY):FlxSprite

Checks the sprite's screen bounds and keeps it entirely within the camera.

Parameters:

sprite

The FlxSprite to restrict.

camera

The camera resitricting the sprite. If left null, FlxG.camera is used.

edges
The edges to restrict. Use constants like LEFT, RIGHT, `UPDOWN or ANY`.

Returns:

The FlxSprite for chaining

Available since

4.11.0

.

@:value({ edges : ANY })staticcameraWrap(sprite:FlxSprite, ?camera:FlxCamera, edges:FlxDirectionFlags = ANY):FlxSprite

Checks the sprite's screen bounds of the FlxSprite and keeps them within the camera by wrapping it around.

Parameters:

sprite

The FlxSprite to wrap.

camera

The camera to wrap around. If left null, FlxG.camera is used.

edges
The edges FROM which to wrap. Use constants like LEFT, RIGHT, `UPDOWN or ANY`.

Returns:

The FlxSprite for chaining

Available since

4.11.0

.

@:value({ FillColor : FlxColor.WHITE, Radius : -1, Y : -1, X : -1 })staticdrawCircle(sprite:FlxSprite, X:Float = -1, Y:Float = -1, Radius:Float = -1, FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:Null<LineStyle>, ?drawStyle:Null<DrawStyle>):FlxSprite

This function draws a circle on a FlxSprite at position X,Y with the specified color.

Parameters:

sprite

The FlxSprite to manipulate

X

X coordinate of the circle's center (automatically centered on the sprite if -1)

Y

Y coordinate of the circle's center (automatically centered on the sprite if -1)

Radius

Radius of the circle (makes sure the circle fully fits on the sprite's graphic if < 1, assuming and and y are centered)

FillColor

The ARGB color to fill this circle with. FlxColor.TRANSPARENT (0x0) means no fill.

lineStyle

A LineStyle typedef containing the params of Graphics.lineStyle()

drawStyle

A DrawStyle typedef containing the params of BitmapData.draw()

Returns:

The FlxSprite for chaining

@:value({ FillColor : FlxColor.TRANSPARENT })staticdrawCurve(sprite:FlxSprite, StartX:Float, StartY:Float, EndX:Float, EndY:Float, ControlX:Float, ControlY:Float, FillColor:FlxColor = FlxColor.TRANSPARENT, ?lineStyle:Null<LineStyle>, ?drawStyle:Null<DrawStyle>):FlxSprite

This function draws a curve on a FlxSprite from position X1,Y1 to anchor position X2,Y2 using control points X3,Y3 with the specified color.

Parameters:

sprite

The FlxSprite to manipulate

StartX

X coordinate of the curve's start point.

StartY

Y coordinate of the curve's start point.

EndX

X coordinate of the curve's end/anchor point.

EndY

Y coordinate of the curve's end/anchor point.

ControlX

X coordinate of the curve's control point.

ControlY

Y coordinate of the curve's control point.

FillColor

The ARGB color to fill this curve with. FlxColor.TRANSPARENT (0x0) means no fill. Filling a curve draws a line from End to Start to complete the figure.

lineStyle

A LineStyle typedef containing the params of Graphics.lineStyle()

drawStyle

A DrawStyle typedef containing the params of BitmapData.draw()

Returns:

The FlxSprite for chaining

@:value({ FillColor : FlxColor.WHITE })staticdrawEllipse(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float, FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:Null<LineStyle>, ?drawStyle:Null<DrawStyle>):FlxSprite

This function draws an ellipse on a FlxSprite.

Parameters:

sprite

The FlxSprite to manipulate

X

X coordinate of the ellipse's start point.

Y

Y coordinate of the ellipse's start point.

Width

Width of the ellipse

Height

Height of the ellipse

FillColor

The ARGB color to fill this ellipse with. FlxColor.TRANSPARENT (0x0) means no fill.

lineStyle

A LineStyle typedef containing the params of Graphics.lineStyle()

drawStyle

A DrawStyle typedef containing the params of BitmapData.draw()

Returns:

The FlxSprite for chaining

staticdrawLine(sprite:FlxSprite, StartX:Float, StartY:Float, EndX:Float, EndY:Float, ?lineStyle:Null<LineStyle>, ?drawStyle:Null<DrawStyle>):FlxSprite

This function draws a line on a FlxSprite from position X1,Y1 to position X2,Y2 with the specified color.

Parameters:

sprite

The FlxSprite to manipulate

StartX

X coordinate of the line's start point.

StartY

Y coordinate of the line's start point.

EndX

X coordinate of the line's end point.

EndY

Y coordinate of the line's end point.

lineStyle

A LineStyle typedef containing the params of Graphics.lineStyle()

drawStyle

A DrawStyle typedef containing the params of BitmapData.draw()

Returns:

The FlxSprite for chaining

@:value({ FillColor : FlxColor.WHITE })staticdrawPolygon(sprite:FlxSprite, Vertices:Array<FlxPoint>, FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:Null<LineStyle>, ?drawStyle:Null<DrawStyle>):FlxSprite

This function draws a polygon on a FlxSprite.

Parameters:

sprite

The FlxSprite to manipulate

Vertices

Array of Vertices to use for drawing the polygon

FillColor

The ARGB color to fill this polygon with. FlxColor.TRANSPARENT (0x0) means no fill.

lineStyle

A LineStyle typedef containing the params of Graphics.lineStyle()

drawStyle

A DrawStyle typedef containing the params of BitmapData.draw()

Returns:

The FlxSprite for chaining

@:value({ FillColor : FlxColor.WHITE })staticdrawRect(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float, FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:Null<LineStyle>, ?drawStyle:Null<DrawStyle>):FlxSprite

This function draws a rectangle on a FlxSprite.

Parameters:

sprite

The FlxSprite to manipulate

X

X coordinate of the rectangle's start point.

Y

Y coordinate of the rectangle's start point.

Width

Width of the rectangle

Height

Height of the rectangle

FillColor

The ARGB color to fill this rectangle with. FlxColor.TRANSPARENT (0x0) means no fill.

lineStyle

A LineStyle typedef containing the params of Graphics.lineStyle()

drawStyle

A DrawStyle typedef containing the params of BitmapData.draw()

Returns:

The FlxSprite for chaining

@:value({ FillColor : FlxColor.WHITE })staticdrawRoundRect(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float, EllipseWidth:Float, EllipseHeight:Float, FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:Null<LineStyle>, ?drawStyle:Null<DrawStyle>):FlxSprite

This function draws a rounded rectangle on a FlxSprite.

Parameters:

sprite

The FlxSprite to manipulate

X

X coordinate of the rectangle's start point.

Y

Y coordinate of the rectangle's start point.

Width

Width of the rectangle

Height

Height of the rectangle

EllipseWidth

The width of the ellipse used to draw the rounded corners

EllipseHeight

The height of the ellipse used to draw the rounded corners

FillColor

The ARGB color to fill this rectangle with. FlxColor.TRANSPARENT (0x0) means no fill.

lineStyle

A LineStyle typedef containing the params of Graphics.lineStyle()

drawStyle

A DrawStyle typedef containing the params of BitmapData.draw()

Returns:

The FlxSprite for chaining

@:value({ FillColor : FlxColor.WHITE })staticdrawRoundRectComplex(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float, TopLeftRadius:Float, TopRightRadius:Float, BottomLeftRadius:Float, BottomRightRadius:Float, FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:Null<LineStyle>, ?drawStyle:Null<DrawStyle>):FlxSprite

This function draws a rounded rectangle on a FlxSprite. Same as drawRoundRect, except it allows you to determine the radius of each corner individually.

Parameters:

sprite

The FlxSprite to manipulate

X

X coordinate of the rectangle's start point.

Y

Y coordinate of the rectangle's start point.

Width

Width of the rectangle

Height

Height of the rectangle

TopLeftRadius

The radius of the top left corner of the rectangle

TopRightRadius

The radius of the top right corner of the rectangle

BottomLeftRadius

The radius of the bottom left corner of the rectangle

BottomRightRadius

The radius of the bottom right corner of the rectangle

FillColor

The ARGB color to fill this rectangle with. FlxColor.TRANSPARENT (0x0) means no fill.

lineStyle

A LineStyle typedef containing the params of Graphics.lineStyle()

drawStyle

A DrawStyle typedef containing the params of BitmapData.draw()

Returns:

The FlxSprite for chaining

@:value({ FillColor : FlxColor.WHITE })staticdrawTriangle(sprite:FlxSprite, X:Float, Y:Float, Height:Float, FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:Null<LineStyle>, ?drawStyle:Null<DrawStyle>):FlxSprite

This function draws a simple, equilateral triangle on a FlxSprite.

Parameters:

sprite

The FlxSprite to manipulate

X

X position of the triangle

Y

Y position of the triangle

Height

Height of the triangle

FillColor

The ARGB color to fill this triangle with. FlxColor.TRANSPARENT (0x0) means no fill.

lineStyle

A LineStyle typedef containing the params of Graphics.lineStyle()

drawStyle

A DrawStyle typedef containing the params of BitmapData.draw()

Returns:

The FlxSprite for chaining

staticinlineendDraw(sprite:FlxSprite, ?drawStyle:Null<DrawStyle>):FlxSprite

Helper function that the drawing functions use at the end.

Parameters:

sprite

The FlxSprite to draw to

drawStyle

A DrawStyle typedef containing the params of BitmapData.draw()

Returns:

The FlxSprite for chaining

@:value({ Duration : 1 })staticinlinefadeIn(sprite:FlxSprite, Duration:Float = 1, ?ResetAlpha:Bool, ?OnComplete:Null<TweenCallback>):FlxSprite

Fade in a sprite, tweening alpha to 1.

Parameters:

sprite

The object to fade.

Duration

How long the fade will take (in seconds).

Returns:

The FlxSprite for chaining

@:value({ Duration : 1 })staticinlinefadeOut(sprite:FlxSprite, Duration:Float = 1, ?OnComplete:Null<TweenCallback>):FlxSprite

Fade out a sprite, tweening alpha to 0.

Parameters:

sprite

The object to fade.

Duration

How long the fade will take (in seconds).

Returns:

The FlxSprite for chaining

staticfill(sprite:FlxSprite, FillColor:FlxColor):FlxSprite

Fills this sprite's graphic with a specific color.

Parameters:

Sprite

The FlxSprite to manipulate

FillColor

The color with which to fill the graphic, format 0xAARRGGBB.

Returns:

The FlxSprite for chaining

@:value({ duration : 0.5, tint : FlxColor.WHITE })staticinlineflashTint(sprite:FlxSprite, tint:FlxColor = FlxColor.WHITE, duration:Float = 0.5, ?func:Float ‑> Float, ?onComplete:() ‑> Void):FlxSprite

Uses FlxTween.num to call setTint on the target sprite

Parameters:

tint

The color to tint the sprite, where alpha determines the max strength

duration

How long the flash lasts

func

Controls the amount of tint over time. The input float goes from 0 to 1.0, an output of 1.0 means the tint is fully applied. If omitted, (n)->1-n is used, meaning it starts at full tint and fades away

onComplete

Called when the flash is complete

Available since

5.4.0

.

@:value({ ForceRestart : true, EndVisibility : true, Interval : 0.04, Duration : 1 })staticinlineflicker(Object:FlxObject, Duration:Float = 1, Interval:Float = 0.04, EndVisibility:Bool = true, ForceRestart:Bool = true, ?CompletionCallback:FlxFlicker ‑> Void, ?ProgressCallback:FlxFlicker ‑> Void):FlxFlicker

A simple flicker effect for sprites achieved by toggling visibility.

Parameters:

Object

The sprite.

Duration

How long to flicker for (in seconds). 0 means "forever".

Interval

In what interval to toggle visibility. Set to FlxG.elapsed if <= 0!

EndVisibility

Force the visible value when the flicker completes, useful with fast repetitive use.

ForceRestart

Force the flicker to restart from beginning, discarding the flickering effect already in progress if there is one.

CompletionCallback

An optional callback that will be triggered when a flickering has finished.

ProgressCallback

An optional callback that will be triggered when visibility is toggled.

Returns:

The FlxFlicker object. FlxFlickers are pooled internally, so beware of storing references.

staticinlinegetDefaultLineStyle(?lineStyle:Null<LineStyle>):LineStyle

Helper function for the default line styles of drawLine() and drawCurve()

Parameters:

lineStyle

The lineStyle typedef

staticinlineisFlickering(Object:FlxObject):Bool

Returns whether an object is flickering or not.

Parameters:

Object

The object to check against.

@:value({ Bottom : true, Top : true, Right : true, Left : true })staticscreenWrap(sprite:FlxSprite, Left:Bool = true, Right:Bool = true, Top:Bool = true, Bottom:Bool = true):FlxSprite

Checks the x/y coordinates of the FlxSprite and keeps them within the area of 0, 0, FlxG.width, FlxG.height (i.e. wraps it around the screen)

Parameters:

sprite

The FlxSprite to keep within the screen

Left

Whether to activate screen wrapping on the left side of the screen

Right

Whether to activate screen wrapping on the right side of the screen

Top

Whether to activate screen wrapping on the top of the screen

Bottom

Whether to activate screen wrapping on the bottom of the screen

Returns:

The FlxSprite for chaining

staticinlinesetBrightness(sprite:FlxSprite, brightness:Float):Void

Change's this sprite's color transform to brighten or darken it. Mimics Adobe Animate's "Brightness" color effect

Parameters:

brightness

Use 1.0 to fully brighten, -1.0 to fully darken, or anything inbetween

Available since

5.4.0

.

@:noUsingstaticinlinesetLineStyle(lineStyle:LineStyle):Void

Just a helper function that is called in the draw functions to set the lineStyle via Graphics.lineStyle()

Parameters:

lineStyle

The lineStyle typedef

staticinlinesetTint(sprite:FlxSprite, tint:FlxColor):Void

Change's this sprite's color transform to apply a tint effect. Mimics Adobe Animate's "Tint" color effect

Parameters:

tint

The color to tint the sprite, where alpha determines the strength

Available since

5.4.0

.

@:value({ spaceFromBounds : false })staticspace(objects:Array<FlxObject>, startX:Float, startY:Float, ?horizontalSpacing:Float, ?verticalSpacing:Float, spaceFromBounds:Bool = false, ?position:(FlxObject, Float, Float) ‑> Void):Void

Aligns a set of FlxObjects so there is equal spacing between them

Parameters:

objects

An Array of FlxObjects

startX

The base X coordinate to start the spacing from

startY

The base Y coordinate to start the spacing from

horizontalSpacing

The amount of pixels between each sprite horizontally. Set to null to just keep the current X position of each object.

verticalSpacing

The amount of pixels between each sprite vertically. Set to null to just keep the current Y position of each object.

spaceFromBounds

If set to true the h/v spacing values will be added to the width/height of the sprite, if false it will ignore this

position

A function with the signature (target:FlxObject, x:Float, y:Float):Void. You can use this to tween objects into their spaced position, etc.

staticinlinestopFlickering(Object:FlxObject):FlxObject

Stops flickering of the object. Also it will make the object visible.

Parameters:

Object

The object to stop flickering.

Returns:

The FlxObject for chaining

staticupdateSpriteGraphic(sprite:FlxSprite, ?drawStyle:Null<DrawStyle>):FlxSprite

Just a helper function that is called at the end of the draw functions to handle a few things related to updating a sprite's graphic.

Parameters:

Sprite

The FlxSprite to manipulate

drawStyle

A DrawStyle typedef containing the params of BitmapData.draw()

Returns:

The FlxSprite for chaining