Hey everyone, Kino here. Today we’re showing off how to do pathing in Flixel. Yes, Flixel does have pathing out of the box. But, what is pathing, and how do we do it?

Pathing

Pathing is self explanatory, it follows a set of points to the goal. This can be useful for setting up movement routes, patrol patterns following the player and more. By using pathing, we can even setup more intricate cut scenes or moving platforms in a game. These are all great uses of pathing. Now, how do we use it in our own code?

Using Pathing

To use pathing, we have one option when it comes to FlxSprites. Every FlxSprite has a property on it for creating FlxPaths. This path property, allows you to create paths that a sprite can follow. All we have to do is pass it an array of FlxPoints. Here’s an example of me implementing it in my own game.

import flixel.util.FlxPath;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.util.FlxColor;
class Player extends FlxSprite {
public var walkPath:Array<FlxPoint>;
public function new(x:Float, y:Float) {
super(x, y);
makeGraphic(16, 16, FlxColor.WHITE, true);
walkPath = [new FlxPoint(0, 0), new FlxPoint(100, 100)];
//Creates a new FlxPath for the player to follow
this.path = new FlxPath(walkPath);
//Starts the player moving along the path and back again without stopping.
this.path.start(null, 50, FlxPath.LOOP_FORWARD);
}
}
view raw PathingExample.hx hosted with ❤ by GitHub

Here’s the result in game!

Pretty straight forward right? The cool thing is that they support different types of pathing such as forward, ping pong, backward, and so on. What that means is you could have the enemies in your game follow the path infinitely or maybe stop at the end or go back and forth between two points. A great tool to have when trying to create more complex game mechanics. The less code you have to write yourself the better, am I right?

Conclusion

With that said, good luck game making!! You can find the example project here!!

Example Project

You can also play it here in the live example: Live Example.

If you have any questions, you can contact me on Twitter or subscribe to the blog for more tutorials on Haxe and Haxe Flixel!

%d bloggers like this: