Previous Phaser tutorials:
Phaser tutorial: sprites and custom properties for atlas frames
Phaser tutorial: manage different screen sizes
Phaser tutorial: How to wrap bitmap text
Phaser has many easing functions you can use for tweens. But it may happen, that none of them is what you need. Secondly, default easing functions are not possible to parametrize. Fortunately, it is quite easy to write your own easing function and also create ones that allow you to use additional input parameters.
Gravity bounce easing
In this tutorial I will create "Gravity bounce" easing function. It will produce gravity-like bouncing and it will be possible to parametrize it with three arguments:
- number of bounces (or -1 if we let the the function calculate it based on other values),
- elasticity (or -1 if we let the function calculate it based on other values),
- start from top, if we want first bounce start from top, which means it is half-bounce
Below you can see it in action. Four usages of final function are shown there - just press 1, 2, 3 or 4:
Tweens and easing functionsLet's create simple tween:
This tween moves "target", which can be for example sprite, to x position 150 in 1 second (1000 ms). The used easing function is Phaser.Easing.Linear.None. If you look into Phaser source tween\Easing.js you can see that this function is defined like this:
Other easing function follows it. Each one is taking just one parameter "k". This parameter is between 0 and 1 and function is called from update function in tween\TweenData.js like this:
So, if you want to create you own easing function and use it with tweens you have to create function that takes "k" between 0 and 1 and returns also number between 0 and 1 (ok, you can violate this and return values out of this range to "overshoot"). Unfortunately, this does not allow us to pass some additional parameters to the function. We also have to solve context issue.
We can do this:
We are passing "wrapper" function to tween that satisfies request for "k" parameter, but it just wraps call to another function that takes additional parameters from variables. This will work, but we can do better. First, we can encapsulate parameters in object and second, we can split easing function workload between some initialization part which is called only once (in constructor) and second part that is called during tween update.
Gravity bounce class
This is structure of Gravity easing class with empty methods (for detail see source in final project, or read detailed article here (explained C++ source)):
Class takes initial parameters in constructor. It does quite a heavy calculations on number of bounces, elasticity and so on. It stores several arrays with bounce parameters like duration, height, velocity. Thanks to this the final easing function is then fast. More, the easing function itself does not store or change anything - it just says what output in range 0 - 1 is for input 0 - 1, so it can be shared among multiple objects. In constructor you say for example: "I want gravity bounce function, that will bounce 3 times, elasticity will be 0.25 and I want the first bounce start from top". Constructor does all the calculations and you can then use easing() function in this object for any number of tweens.
We can then use this class where we want. In final project see boot.ts file. There are created four private variables, that store four different gravity bounce objects:
Each is initialized with different parameters in create() function:
Tweens are then created like this:
In the end we have reusable object with easing function that simulates real world gravity bouncing. It is flexible as we can parametrize it with number of bounces or elasticity (or both). It has initialization part, that does all the heavy calculations and easing function itself is fast.
Download final project here: CustomEasing.zip