The Phaser Line constructor
In this post I will be writing about the phaser Line Constructor. This constructor may prove to be somewhat useful when doing anything involving line segments.
Quick Phaser Line Hello World.
To create a line segment I just need to pass the four arguments for the x, and y positions of the two points that make up the Line. To draw the line I can use Phasers Graphics display objects to get that done.
Each line has a start point, and an end point and that is where the line.start, and that is of course where the line.start, and line.end objects come into play. Now that I have the basics done, it would be nice to know about some of the methods that are available for Lines.
Get the midpoint of a Line
This method will return a Point that is the midPoint of the line that I call the method on.
start and end
Every Line has a start and end point getting this is a simple as just cheking out these objects in the instance of Line.
In addition to being helpful just because they are objects that have the properties I often need, both of these Objects are instances of Phaser.Point, and as such have all the properties, and method of that constructor as well.
top, bottom, left, and right
As the names imply these properties simply just give the boundaries of the line.
Get the length of a line
If I have an instance of Line there is no need to use the distance formula to get it’s length, there is all ready a property for that.
Find the angle that the line is heading
Another great thing about an instance of Line is that I can use it to find the angle a line is heading from it’s start point to end point by just checking the angle property
Find if two lines intersect, and if so where
There are some methods attached to the constructor function that are not part of an instance one of theme is the interest methods which comes in handy for collision detection, or to just find a certain point at which two lines meet.
This method will return null if an intersection is not found.
center a line on a given point with centerOn()
The Line constructor Is very helpful when working with lines, just about all the methods I can think of are there. It’s two bad I ran into some problems with a few of the methods, when it comes to finding if a point is on a line or not, but disappointments with phaser are rare so far as I explore Phaser.