class FlxObject
package flixel
extends FlxBasic
extended by FlxSpineCollider, FlxClickArea, FlxBaseTilemap, FlxTile, FlxSprite
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 maximumvelocity
(or negativevelocity
) this object can have.-
angle
: The orientation, in degrees, of thisobject
. Does not affect collision, mainlyused 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
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.
staticdefaultMoves:Bool = true
The default moves
value of all future FlxObjects
and FlxSprites
Note: Has no effect on FlxTexts
, FlxTilemaps
and FlxTileBlocks
5.6.0
.staticdefaultPixelPerfectPosition:Bool = false
Default value for FlxObject
's pixelPerfectPosition
var.
Static methods
staticupdateTouchingFlags(Object1:FlxObject, Object2:FlxObject):Bool
staticupdateTouchingFlagsX(object1:FlxObject, object2:FlxObject):Bool
Constructor
Variables
read onlyacceleration:FlxPoint
How fast the speed of this object is changing (in pixels per second). Useful for smooth movement and gravity.
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.
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!
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
4.11.0
.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
4.11.0
.collisonXDrag:Bool
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.
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).
debugBoundingBoxColorNotSolid:FlxColor = FlxColor.BLUE
Color used for the debug rect if allowCollisions == NONE
.
4.2.0
.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
).
4.2.0
.debugBoundingBoxColorSolid:FlxColor = FlxColor.RED
Color used for the debug rect if allowCollisions == ANY
.
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.
elasticity:Float = 0
The bounciness of this object. Only affects collisions. Default value is 0, or "not bouncy at all."
health:Float = 1
Handy for storing health percentage or armor points or whatever.
height:Float
The height of this object's hitbox. For sprites, use offset
to control the hitbox position.
ignoreDrawDebug:Bool = false
Setting this to true
will prevent the object's bounding box from appearing
when FlxG.debugger.drawDebug
is true
.
read onlylast:FlxPoint
Important variable for collision processing.
By default this value is set automatically during at the start of update()
.
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!
read onlymaxVelocity:FlxPoint
If you are using acceleration
, you can use maxVelocity
with it
to cap the speed automatically (very useful!).
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
.
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.
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.
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!
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!
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
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 |
---|
Returns:
A globally aligned FlxRect
that fully contains the input object's width and height.
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 |
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 |
---|
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. |
---|
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. |
---|
Returns:
Whether the object just landed on (any of) the specified surface(s) this frame.
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 |
camera | Specify which game camera you want.
If |
Returns:
Whether or not the two objects overlap.
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 |
camera | Specify which game camera you want.
If |
Returns:
Whether or not the two objects overlap.
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 |
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. |
inlinescreenCenter(axes:FlxAxes = XY):FlxObject
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 |