This is a simple path data container. Basically a list of points that a FlxObject can follow. Also has code for drawing debug visuals. FlxTilemap.findPath() returns a path usable by FlxPath, but you can also just make your own, using the add() functions below or by creating your own array of points.

Every FlxObject has a path property which can make it move along specified array of way points. Usage example:

var path = new FlxPath();
var points:Array<FlxPoint> = [new FlxPoint(0, 0), new FlxPoint(100, 0)];
object.path = path;
path.start(points, 50, FlxPath.FORWARD);

You can also do this in one line:

object.path = new FlxPath().start([new FlxPoint(0, 0), new FlxPoint(100, 0)], 50, FlxPath.FORWARD);

...or using some more chaining:

object.path = new FlxPath().add(0, 0).add(100, 0).start(50, FlxPath.FORWARD);

If you are fine with the default values of start (speed, mode, auto-rotate) you can also do:

object.path = new FlxPath([new FlxPoint(0, 0), new FlxPoint(100, 0)]).start();

Static variables

@:value(0x000001)staticinlineread onlyBACKWARD:Int = 0x000001

Path behavior controls: move from the end of the path to the start then stop.

@:value(0x000000)staticinlineread onlyFORWARD:Int = 0x000000

Path behavior controls: move from the start of the path to the end then stop.

@:value(0x010000)staticinlineread onlyHORIZONTAL_ONLY:Int = 0x010000

Path behavior controls: ignores any vertical component to the path data, only follows side to side.

@:value(0x000100)staticinlineread onlyLOOP_BACKWARD:Int = 0x000100

Path behavior controls: move from the end of the path to the start then directly back to the end, and start over.

@:value(0x000010)staticinlineread onlyLOOP_FORWARD:Int = 0x000010

Path behavior controls: move from the start of the path to the end then directly back to the start, and start over.

@:value(0x100000)staticinlineread onlyVERTICAL_ONLY:Int = 0x100000

Path behavior controls: ignores any horizontal component to the path data, only follows up and down.

@:value(0x001000)staticinlineread onlyYOYO:Int = 0x001000

Path behavior controls: move from the start of the path to the end then turn around and go back to the start, over and over.

Constructor

new(?Nodes:Array<FlxPoint>)

Variables

@:value(false)active:Bool = false

Pauses or checks the pause state of the path.

@:value(0)angle:Float = 0

The angle in degrees between this object and the next node, where 0 is directly upward, and 90 is to the right.

@:value(true)autoCenter:Bool = true

Whether the object should auto-center the path or at its origin.

@:value(0xffffff)debugColor:FlxColor = 0xffffff

Specify a debug display color for the path. Default is white.

@:value(false)read onlyfinished:Bool = false

@:value(false)ignoreDrawDebug:Bool = false

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

@:value(0)read onlynodeIndex:Int = 0

Tracks which node of the path this object is currently moving toward.

nodes:Array<FlxPoint>

The list of FlxPoints that make up the path data.

@:value(0)speed:Float = 0

The speed at which the object is moving on the path. When an object completes a non-looping path circuit, the path's speed will be zeroed out, but the path reference will NOT be nulled out. So speed is a good way to check if this object is currently following a path or not.

Methods

add(X:Float, Y:Float):FlxPath

Add a new node to the end of the path at the specified location.

Parameters:

X

X position of the new path point in world coordinates.

Y

Y position of the new path point in world coordinates.

Returns:

This path object.

addAt(X:Float, Y:Float, Index:Int):FlxPath

Add a new node to the path at the specified location and index within the path.

Parameters:

X

X position of the new path point in world coordinates.

Y

Y position of the new path point in world coordinates.

Index

Where within the list of path nodes to insert this new point.

Returns:

This path object.

@:value({ AsReference : false })addPoint(Node:FlxPoint, AsReference:Bool = false):FlxPath

Sometimes its easier or faster to just pass a point object instead of separate X and Y coordinates. This also gives you the option of not creating a new node but actually adding that specific FlxPoint object to the path. This allows you to do neat things, like dynamic paths.

Parameters:

Node

The point in world coordinates you want to add to the path.

AsReference

Whether to add the point as a reference, or to create a new point with the specified values.

Returns:

This path object.

@:value({ AsReference : false })addPointAt(Node:FlxPoint, Index:Int, AsReference:Bool = false):FlxPath

Sometimes its easier or faster to just pass a point object instead of separate X and Y coordinates. This also gives you the option of not creating a new node but actually adding that specific FlxPoint object to the path. This allows you to do neat things, like dynamic paths.

@return This path object.

Parameters:

Node

The point in world coordinates you want to add to the path.

Index

Where within the list of path nodes to insert this new point.

AsReference

Whether to add the point as a reference, or to create a new point with the specified values.

cancel():FlxPath

Stops the path's movement.

Returns:

This path object.

destroy():Void

Clean up memory.

@:access(flixel.FlxCamera)drawDebug(?Camera:FlxCamera):Void

While this doesn't override FlxBasic.drawDebug(), the behavior is very similar. Based on this path data, it draws a simple lines-and-boxes representation of the path if the drawDebug mode was toggled in the debugger overlay. You can use debugColor to control the path's appearance.

Parameters:

Camera

The camera object the path will draw to.

head():FlxPoint

Get the first node in the list.

Returns:

The first node in the path.

remove(Node:FlxPoint):FlxPoint

Remove a node from the path. NOTE: only works with points added by reference or with references from nodes itself!

Parameters:

Node

The point object you want to remove from the path.

Returns:

The node that was excised. Returns null if the node was not found.

removeAt(Index:Int):FlxPoint

Remove a node from the path using the specified position in the list of path nodes.

Parameters:

Index

Where within the list of path nodes you want to remove a node.

Returns:

The node that was excised. Returns null if there were no nodes in the path.

reset():FlxPath

Just resets some debugging related variables (for debugger renderer). Also resets autoCenter to true.

Returns:

This path object.

restart():FlxPath

Restarts this path. So object starts movement again from the first (or last) path point (depends on path movement behavior mode).

Returns:

This path object.

setNode(NodeIndex:Int):FlxPath

Change the path node this object is currently at.

Parameters:

NodeIndex

The index of the new node out of path.nodes.

@:value({ AutoRotate : false, Mode : FlxPath.FORWARD, Speed : 100 })setProperties(Speed:Float = 100, Mode:Int = FlxPath.FORWARD, AutoRotate:Bool = false):FlxPath

Sets the following properties: speed, mode and auto rotation.

Parameters:

Speed

The speed at which the object is moving on the path.

Mode

Path following behavior (like looping, horizontal only, etc).

AutoRotate

Whether the object's angle should be adjusted to the path angle during path follow behavior.

Returns:

This path object.

Available since

4.2.0

.

@:value({ NodesAsReference : false, AutoRotate : false, Mode : FlxPath.FORWARD, Speed : 100 })start(?Nodes:Array<FlxPoint>, Speed:Float = 100, Mode:Int = FlxPath.FORWARD, AutoRotate:Bool = false, NodesAsReference:Bool = false):FlxPath

Starts movement along specified path.

Parameters:

Nodes

An optional array of path waypoints. If null then previously added points will be used. Movement is not started if the resulting array has no points.

Speed

The speed at which the object is moving on the path.

Mode

Path following behavior (like looping, horizontal only, etc).

AutoRotate

Whether the object's angle should be adjusted to the path angle during path follow behavior.

NodesAsReference

Whether to pass the input array as reference (true) or to copy the points (false). Default is false.

Returns:

This path object.

tail():FlxPoint

Get the last node in the list.

Returns:

The last node in the path.

update(elapsed:Float):Void

Internal function for moving the object along the path. The first half of the function decides if the object can advance to the next node in the path, while the second half handles actually picking a velocity toward the next node.