In this point on Phaser ce, I will be writing about finding the angle between two sprites using the Point class. looking over my content on phaser so far I am surprised that I do not have any content on the point class, so lets put and end to that with one of the most useful methods that is at the ready in that class.
So lets start this post off with some very basic examples of using the angle method of the phaser point class to find the angle between two points. These two points can be instances of Phaser.Point, a display object such as a Sprite, or just a plain old object with x, and y properties.
This section will provide some very simple copy and past demos to help get a feel as to how to work with this method in different ways.
The call method of the function prototype is a very powerful method, if you are not familiar with call yet as a jaavScript developer you should get up to speed with it. In a nut shell it allows me to call methods of any prototype, on any object, and if that object has the properties that the method needs, it might very well work.
Call works by accepting the first argument that I give it as the value of the this keyword. Because Phaser.prototype.angle is a prototype method of Point the first argument that I give it should be some kind object that has proprieties that are similar to that of an instance of Point, namely x, and y properties. The next argument that i give it will be the first argument that I would give it if I was calling it on an instance of Point.
If that all confuses you just check out this example:
So in this example I am finding the angle between these two plain old objects, that have x, and y properties by using the call Function prototype method. The angle that is returned is in the range of Math.PI to -Math.PI so if I want degrees i will need to convert.
So for a very Basic example involving two sprites I put together this example in which I make two sprites, and then create an instance of Point with the new keyword. Nothing special here with call, just using the Point.prototype.angle method as intended now here.
So then creating an instance of Phaser.Point is fairly easy, I just need to pass the x, and y properties to the constructor when calling it with the new keyword to get a new instance of Phaser.Point. Once I have My instance Of Point there are all kinds of useful methods that I can call off of it, including Phaser.Point.angle.
The Point Class has many more useful methods that I hope to write content on, as I work on expanding, and updating my content on phaser this Month. Needless to say this method will be of use for many typically scenarios that come up when making a project with Phaser. I hope you found this post helpful, if not be sure to tell me what Might be missing in the comments, and be sure to have fun with phaser.