Playing with acceleration in phaser ce
For todays post on phaser ce I thought I would play around with acceleration. Doing so with phaser ce is just a matter of setting point values for the instance of Phaser.Point at Sprite.body.acceleration in a physics enabled Sprite. In this post I will be covering a simple silly use case example of how to go about working with acceleration in phaser ce, and also touch base on some other important tools available in the framework for doing so.
1 - What to know before continuing.
This is a post on setting acceleration for a sprite physics body in phaser ce. It is not a getting started post on physics in phaser ce, a getting started post with phaser, or with javaScript in general. To gain anything of value from this post it is important to start with the basics first, there is a whole lot to be aware with in phaser before starting to get into the arcade physics engine.
1.1 - This is a phaser ce 2.x post
In this post I am using phaser community edition 2.11.1 of phaser.
2 - Accelerating a ball based on distance from a point
In this example I am changing acceleration of a ball based on the distance and angle to another point of reference at the center of the game world. So in the post on top of using the Phaser.Point instance at Sprite.body.acceleration, I am also using Point methods like Point.angle, and Point.distance as well. This does not aim to be a practical example, just an exercise. In game projects acceleration is something that might be set with user input, or hitting a power up of some kind. In any case it will involve setting values at the Point instance at Sprite.body.acceleration.
2.1 - The updateAccleration method
Here is the method that is used to update acceleration on each frame tick.
|
|
2.2 - The Create Ball method
In the method I create the ball sprite, and enable physics for the sprite.
|
|
2.3 - Create the Ball Sprite Sheet
Here I am creating a simple sprite sheet to be used to skin the sprite with.
|
|
2.4 - The Phaser.Game instance
Now that I have everything worked out I just need to get it all working with a Phaser.Game instance, and a single state object. I call my CreateBallSheet, and createBall methods in the create method of a state object to set things up. In addition I of course call my updateAccleration method in the update method as well.
|
|
3 - Conclusion
Acceleration is one of many other factors to take into account when starting to play with physics in phaser ce. As I work on expanding and improving my content on phaser I will likely want to expand more on this post on a later date, and provide at least one more example involving acceleration in phaser ce. If you have any suggestions for an additional example, let me know in the comments, and that you for reading.