At their core FlxObjects are just boxes with positions that can move and collide with other objects. Most games utilize FlxObject's features through FlxSprite, which extends FlxObject directly and adds graphical capabilities.

Motion

Whenever update is called, objects with move set to true will update their positions based on the following properties: - velocity: The speed of the object in pixels per second. - acceleration: The rate at which velocity will change in pixels per second. - drag: When acceleration is 0, velocity will slow by this amount, in pixels per second.

      When less than or equal to 0, no drag is applied.
  • maxVelocity: The maximum velocity (or negative velocity) this object can have.
  • angle: The orientation, in degrees, of this object. Does not affect collision, mainly

       used for `FlxSprite` graphics.
    
  • angularVelocity: The rotational speed of the object in degrees per second.

Overlaps

If you're only checking an overlap between two objects you can use player.overlaps(door) or player.overlaps(spikeGroup). You can check if two objects or groups of object overlap with FlxG.overlap.

Example:

if (FlxG.overlap(playerGroup, spikeGroup)) trace("overlap!");

You can also specify a callback to handle which specific objects collided:

FlxG.overlap(playerGroup, medKitGroup
    function onOverlap(player, medKit)
    {
        player.health = 100;
        medKit.kill();
    }
);

Additional resources: - Snippets - Simple Overlap - Snippets - Overlap Callbacks

Collision

FlxG.collide is similar to FlxG.overlap except it resolves the overlap by separating their positions before calling the callback. Typically collide is called on an update loop like so:

FlxG.collide(playerGroup, crateGroup);

This takes the player's and crate's momentum and previous and current position in consideration when resolving overlaps between them. Like overlap collide will return true if any objects were overlapping, and you can specify a callback.

Additional resources: - Snippets - 1 to 1 Collision - Demos - FlxCollisions - Demos - Collision and Grouping

See also:

Static variables

@:value(4)staticSEPARATE_BIAS:Float = 4

This value dictates the maximum number of pixels two objects have to intersect before collision stops trying to separate them. Don't modify this unless your objects are passing through each other.

@:value(true)staticdefaultMoves:Bool = true

The default moves value of all future FlxObjects and FlxSprites Note: Has no effect on FlxTexts, FlxTilemaps and FlxTileBlocks

Available since

5.6.0

.

@:value(false)staticdefaultPixelPerfectPosition:Bool = false

Default value for FlxObject's pixelPerfectPosition var.

Static methods

staticseparate(object1:FlxObject, object2:FlxObject):Bool

The main collision resolution function in Flixel.

Parameters:

Object1

Any FlxObject.

Object2

Any other FlxObject.

Returns:

Whether the objects in fact touched and were separated.

staticseparateX(object1:FlxObject, object2:FlxObject):Bool

The X-axis component of the object separation process.

Parameters:

object1

Any FlxObject.

object2

Any other FlxObject.

Returns:

Whether the objects in fact touched and were separated along the X axis.

staticseparateY(object1:FlxObject, object2:FlxObject):Bool

The Y-axis component of the object separation process.

Parameters:

object1

Any FlxObject.

object2

Any other FlxObject.

Returns:

Whether the objects in fact touched and were separated along the Y axis.

staticupdateTouchingFlags(Object1:FlxObject, Object2:FlxObject):Bool

Similar to separate(), but only checks whether any overlap is found and updates the touching flags of the input objects, but no separation is performed.

Parameters:

Object1

Any FlxObject.

Object2

Any other FlxObject.

Returns:

Whether the objects in fact touched.

staticupdateTouchingFlagsX(object1:FlxObject, object2:FlxObject):Bool

Checking overlap and updating touching variables, X-axis part used by updateTouchingFlags.

Parameters:

object1

Any FlxObject.

object2

Any other FlxObject.

Returns:

Whether the objects in fact touched along the X axis.

staticupdateTouchingFlagsY(object1:FlxObject, object2:FlxObject):Bool

Checking overlap and updating touching variables, Y-axis part used by updateTouchingFlags.

Parameters:

object1

Any FlxObject.

object2

Any other FlxObject.

Returns:

Whether the objects in fact touched along the Y axis.

Constructor

@:value({ height : 0, width : 0, y : 0, x : 0 })new(x:Float = 0, y:Float = 0, width:Float = 0, height:Float = 0)

Parameters:

X

The X-coordinate of the point in space.

Y

The Y-coordinate of the point in space.

Width

Desired width of the rectangle.

Height

Desired height of the rectangle.

Variables

read onlyacceleration:FlxPoint

How fast the speed of this object is changing (in pixels per second). Useful for smooth movement and gravity.

@:value(FlxDirectionFlags.ANY)allowCollisions:FlxDirectionFlags = FlxDirectionFlags.ANY

Bit field of flags (use with UP, DOWN, LEFT, RIGHT, etc) indicating collision directions. Use bitwise operators to check the values stored here. Useful for things like one-way platforms (e.g. allowCollisions = UP;). The accessor "solid" just flips this variable between NONE and ANY.

@:value(0)angle:Float = 0

Set the angle (in degrees) of a sprite to rotate it. WARNING: rotating sprites decreases their rendering performance by a factor of ~10x when using blitting!

@:value(0)angularAcceleration:Float = 0

How fast the spin speed should change (in degrees per second).

@:value(0)angularDrag:Float = 0

Like drag but for spinning.

@:value(0)angularVelocity:Float = 0

This is how fast you want this sprite to spin (in degrees per second).

@:value(IMMOVABLE)collisionXDrag:CollisionDragType = IMMOVABLE

Whether this sprite is dragged along with the horizontal movement of objects it collides with (makes sense for horizontally-moving platforms in platformers for example). Use values IMMOVABLE, ALWAYS, HEAVIER or NEVER

Available since

4.11.0

.

@:value(NEVER)collisionYDrag:CollisionDragType = NEVER

Whether this sprite is dragged along with the vertical movement of objects it collides with (for sticking to vertically-moving platforms in platformers for example). Use values IMMOVABLE, ALWAYS, HEAVIER or NEVER

Available since

4.11.0

.

collisonXDrag:Bool

Deprecated: "Use `collisionXDrag`, instead. Note the corrected spelling: `collis(i)onXDrag"

DEPRECATED Whether this sprite is dragged along with the horizontal movement of objects it collides with (makes sense for horizontally-moving platforms in platformers for example).

Apart from having a weird typo, this has been deprecated for collisionXDrag, which allows more options.

@:value(null)debugBoundingBoxColor:Null<FlxColor> = null

Overriding this will force a specific color to be used for debug rect (ignoring any of the other debug bounding box colors specified).

@:value(FlxColor.BLUE)debugBoundingBoxColorNotSolid:FlxColor = FlxColor.BLUE

Color used for the debug rect if allowCollisions == NONE.

Available since

4.2.0

.

@:value(FlxColor.GREEN)debugBoundingBoxColorPartial:FlxColor = FlxColor.GREEN

Color used for the debug rect if this object collides partially (immovable in the case of FlxObject, or allowCollisions not equal to ANY or NONE in the case of tiles in FlxTilemap).

Available since

4.2.0

.

@:value(FlxColor.RED)debugBoundingBoxColorSolid:FlxColor = FlxColor.RED

Color used for the debug rect if allowCollisions == ANY.

Available since

4.2.0

.

read onlydrag:FlxPoint

This isn't drag exactly, more like deceleration that is only applied when acceleration is not affecting the sprite.

@:value(0)elasticity:Float = 0

The bounciness of this object. Only affects collisions. Default value is 0, or "not bouncy at all."

@:value(1)health:Float = 1

Handy for storing health percentage or armor points or whatever.

@:isVarheight:Float

The height of this object's hitbox. For sprites, use offset to control the hitbox position.

@:value(false)ignoreDrawDebug:Bool = false

Setting this to true will prevent the object's bounding box from appearing when FlxG.debugger.drawDebug is true.

@:value(false)immovable:Bool = false

Whether an object will move/alter position after a collision.

read onlylast:FlxPoint

Important variable for collision processing. By default this value is set automatically during at the start of update().

@:value(1)mass:Float = 1

The virtual mass of the object. Default value is 1. Currently only used with elasticity during collision resolution. Change at your own risk; effects seem crazy unpredictable so far!

@:value(10000)maxAngular:Float = 10000

Use in conjunction with angularAcceleration for fluid spin speed control.

read onlymaxVelocity:FlxPoint

If you are using acceleration, you can use maxVelocity with it to cap the speed automatically (very useful!).

@:value(defaultMoves)moves:Bool = defaultMoves

Set this to false if you want to skip the automatic motion/movement stuff (see updateMotion()). FlxObject and FlxSprite default to true. FlxText, FlxTileblock and FlxTilemap default to false.

@:value(null)path:FlxPath = null

The path this object follows. Not initialized by default. Assign a new FlxPath() object and start() it if you want to this object to follow a path. Set path to null again to stop following the path. See flixel.util.FlxPath for more info and usage examples.

@:value(true)pixelPerfectPosition:Bool = true

Whether or not the position of this object should be rounded before any draw() or collision checking.

pixelPerfectRender:Null<Bool>

Whether or not the coordinates should be rounded during rendering. Does not affect copyPixels(), which can only render on whole pixels. Defaults to the camera's global pixelPerfectRender value, but overrides that value if not equal to null.

read onlyscrollFactor:FlxPoint

Controls how much this object is affected by camera scrolling. 0 = no movement (e.g. a background layer), 1 = same movement speed as the foreground. Default value is (1,1), except for UI elements like FlxButton where it's (0,0).

solid:Bool

Whether the object collides or not. For more control over what directions the object will collide from, use collision constants (like LEFT, FLOOR, etc) to set the value of allowCollisions directly.

@:value(FlxDirectionFlags.NONE)touching:FlxDirectionFlags = FlxDirectionFlags.NONE

Bit field of flags (use with UP, DOWN, LEFT, RIGHT, etc) indicating surface contacts. Use bitwise operators to check the values stored here, or use isTouching(), justTouched(), etc. You can even use them broadly as boolean values if you're feeling saucy!

read onlyvelocity:FlxPoint

The basic speed of this object (in pixels per second).

@:value(FlxDirectionFlags.NONE)wasTouching:FlxDirectionFlags = FlxDirectionFlags.NONE

Bit field of flags (use with UP, DOWN, LEFT, RIGHT, etc) indicating surface contacts from the previous game loop step. Use bitwise operators to check the values stored here, or use isTouching(), justTouched(), etc. You can even use them broadly as boolean values if you're feeling saucy!

@:isVarwidth:Float

The width of this object's hitbox. For sprites, use offset to control the hitbox position.

@:value(0)x:Float = 0

X position of the upper left corner of this object in world space.

@:value(0)y:Float = 0

Y position of the upper left corner of this object in world space.

Methods

destroy():Void

WARNING: A destroyed FlxBasic can't be used anymore. It may even cause crashes if it is still part of a group or state. You may want to use kill() instead if you want to disable the object temporarily only and revive() it later.

This function is usually not called manually (Flixel calls it automatically during state switches for all add()ed objects).

Override this function to null out variables manually or call destroy() on class members if necessary. Don't forget to call super.destroy()!

draw():Void

Rarely called, and in this case just increments the visible objects count and calls drawDebug() if necessary.

drawDebugOnCamera(camera:FlxCamera):Void

Override this function to draw custom "debug mode" graphics to the specified camera while the debugger's drawDebug mode is toggled on.

Parameters:

Camera

Which camera to draw the debug visuals to.

getMidpoint(?point:FlxPoint):FlxPoint

Retrieve the midpoint of this object in world coordinates.

Parameters:

point

Allows you to pass in an existing FlxPoint object if you're so inclined. Otherwise a new one is created.

Returns:

A FlxPoint object containing the midpoint of this object in world coordinates.

getPosition(?result:FlxPoint):FlxPoint

Returns the world position of this object.

Parameters:

result

Optional arg for the returning point.

Returns:

The world position of this object.

getRotatedBounds(?newRect:FlxRect):FlxRect

Calculates the smallest globally aligned bounding box that encompasses this object's width and height, at its current rotation. Note, if called on a FlxSprite, the origin is used, but scale and offset are ignored. Use getScreenBounds to use these properties.

Parameters:

newRect

The optional output FlxRect to be returned, if null, a new one is created.

Returns:

A globally aligned FlxRect that fully contains the input object's width and height.

Available since

4.11.0

.

getScreenPosition(?result:FlxPoint, ?camera:FlxCamera):FlxPoint

Returns the screen position of this object.

Parameters:

result

Optional arg for the returning point

camera

The desired "screen" coordinate space. If null, FlxG.camera is used.

Returns:

The screen position of this object.

hurt(damage:Float):Void

Reduces the health variable of this object by the amount specified in Damage. Calls kill() if health drops to or below zero.

Parameters:

Damage

How much health to take away (use a negative number to give a health bonus).

inlineinWorldBounds():Bool

Check and see if this object is currently within the world bounds - useful for killing objects that get too far away.

Returns:

Whether the object is within the world bounds or not.

isOnScreen(?camera:FlxCamera):Bool

Check and see if this object is currently on screen.

Parameters:

camera

Specify which game camera you want. If null, it will just grab the first global camera.

Returns:

Whether the object is on screen or not.

isPixelPerfectRender(?camera:FlxCamera):Bool

Check if object is rendered pixel perfect on a specific camera.

inlineisTouching(direction:FlxDirectionFlags):Bool

Handy function for checking if this object is touching a particular surface. Note: These flags are set from FlxG.collide calls, and get reset in super.update().

Parameters:

direction

Any of the collision flags (e.g. LEFT, FLOOR, etc).

Returns:

Whether the object is touching an object in (any of) the specified direction(s) this frame.

inlinejustTouched(direction:FlxDirectionFlags):Bool

Handy function for checking if this object is just landed on a particular surface. Note: These flags are set from FlxG.collide calls, and get reset in super.update().

Parameters:

direction

Any of the collision flags (e.g. LEFT, FLOOR, etc).

Returns:

Whether the object just landed on (any of) the specified surface(s) this frame.

@:value({ inScreenSpace : false })@:access(flixel.group.FlxTypedGroup)overlaps(objectOrGroup:FlxBasic, inScreenSpace:Bool = false, ?camera:FlxCamera):Bool

Checks to see if some FlxObject overlaps this FlxObject or FlxGroup. If the group has a LOT of things in it, it might be faster to use FlxG.overlap(). WARNING: Currently tilemaps do NOT support screen space overlap checks!

Parameters:

objectOrGroup

The object or group being tested.

inScreenSpace

Whether to take scroll factors into account when checking for overlap. Default is false, or "only compare in world space."

camera

Specify which game camera you want. If null, it will just grab the first global camera.

Returns:

Whether or not the two objects overlap.

@:value({ inScreenSpace : false })@:access(flixel.group.FlxTypedGroup)overlapsAt(x:Float, y:Float, objectOrGroup:FlxBasic, inScreenSpace:Bool = false, ?camera:FlxCamera):Bool

Checks to see if this FlxObject were located at the given position, would it overlap the FlxObject or FlxGroup? This is distinct from overlapsPoint(), which just checks that point, rather than taking the object's size into account. WARNING: Currently tilemaps do NOT support screen space overlap checks!

Parameters:

x

The X position you want to check. Pretends this object (the caller, not the parameter) is located here.

y

The Y position you want to check. Pretends this object (the caller, not the parameter) is located here.

objectOrGroup

The object or group being tested.

inScreenSpace

Whether to take scroll factors into account when checking for overlap. Default is false, or "only compare in world space."

camera

Specify which game camera you want. If null, it will just grab the first global camera.

Returns:

Whether or not the two objects overlap.

@:value({ inScreenSpace : false })overlapsPoint(point:FlxPoint, inScreenSpace:Bool = false, ?camera:FlxCamera):Bool

Checks to see if a point in 2D world space overlaps this FlxObject.

Parameters:

point

The point in world space you want to check.

inScreenSpace

Whether to take scroll factors into account when checking for overlap.

camera

Specify which game camera you want. If null, it will just grab the first global camera.

Returns:

Whether or not the point overlaps this object.

reset(x:Float, y:Float):Void

Handy function for reviving game objects. Resets their existence flags and position.

Parameters:

x

The new X position of this object.

y

The new Y position of this object.

@:value({ axes : XY })inlinescreenCenter(axes:FlxAxes = XY):FlxObject

Centers this FlxObject on the screen, either by the x axis, y axis, or both.

Parameters:

axes

On what axes to center the object (e.g. X, Y, XY) - default is both.

Returns:

This FlxObject for chaining

@:value({ y : 0.0, x : 0.0 })setPosition(x:Float = 0.0, y:Float = 0.0):Void

Helper function to set the coordinates of this object. Handy since it only requires one line of code.

Parameters:

x

The new x position

y

The new y position

setSize(width:Float, height:Float):Void

Shortcut for setting both width and Height.

Parameters:

width

The new hitbox width.

height

The new hitbox height.

toString():String

Convert object to readable string name. Useful for debugging, save games, etc.

update(elapsed:Float):Void

Override this function to update your class's position and appearance. This is where most of your game rules and behavioral code will go.

Inherited Variables

Defined by FlxBasic

@:value(idEnumerator++)ID:Int = idEnumerator++

A unique ID starting from 0 and increasing by 1 for each subsequent FlxBasic that is created.

@:value(true)active:Bool = true

Controls whether update() is automatically called by FlxState/FlxGroup.

@:value(true)alive:Bool = true

Useful state for many game objects - "dead" (!alive) vs alive. kill() and revive() both flip this switch (along with exists, but you can override that).

camera:FlxCamera

Gets or sets the first camera of this object.

cameras:Array<FlxCamera>

This determines on which FlxCameras this object will be drawn. If it is null / has not been set, it uses the list of default draw targets, which is controlled via FlxG.camera.setDefaultDrawTarget as well as the DefaultDrawTarget argument of FlxG.camera.add.

@:value(true)exists:Bool = true

Controls whether update() and draw() are automatically called by FlxState/FlxGroup.

@:value(true)visible:Bool = true

Controls whether draw() is automatically called by FlxState/FlxGroup.

Inherited Methods

Defined by FlxBasic

kill():Void

Handy function for "killing" game objects. Use reset() to revive them. Default behavior is to flag them as nonexistent AND dead. However, if you want the "corpse" to remain in the game, like to animate an effect or whatever, you should override this, setting only alive to false, and leaving exists true.

revive():Void

Handy function for bringing game objects "back to life". Just sets alive and exists back to true. In practice, this function is most often called by FlxObject#reset().