In many phaser ce projects there will come a time where I will want to rotate a sprite from one angle to another. When doing so i will want to have the sprite rotate in a direction that is the shortest angular distance. Thankfully there is a Math Class method in phaser ce that is there for this very purpose called Phaser.Math.rotateToAngle, in this post i will be covering a quick example that makes use of this method.
In this post I am using phaser community edition 2.11.1 of phaser.
So for this example I have two sprites one is a ship, and another is a target. I want the ship to rotate to the target, and do so in a way in which it takes the shortest angular distance clockwise or counter clockwise. To do so I make use of Phaser.Math.rotateToAngle.
Here I have the method that will rotate the ship sprite to the target sprite using Math.rotateToAngle. In addition when it reaches the target it also sets a new target as well.
This helper just sets the position of target sprite to the current target angle that is stores in the ships data object.
A simple helper that makes the sprites.
A simple canvas solution for making a sprite sheet.
Now to get everything to work by creating an instance of Phaser.Game, calling the mkShett, and mkSprite helpers in the create method of a state object. In addition I call my roatteTotarget method on each frame tick in the update method of the state, and of course I start the state.
This results in the ship rotating to the target, at which point a new target is created, and the process repeats as expected.
So this is just one of many such methods in phaser that help with common tasks such as this resulting in me spending less time creating and finding solutions for problems, and spending more time focusing on what sets my project apart from others. If you would like to see more examples of this, or have any questions or concerns be sure to let me know in the comments, and that you for reading.