Phaser 3 Example: Set Gravity And Stop Object From Moving When Clicked

In Phaser 3, we can set gravity for an object to make it falling for floating. I am working on a project where I want to set the gravity but when I click on it, the object should stop moving. Here is a working example of a scene.

export class MainMenuScene extends BaseScene {

    private Baloon: Phaser.Types.Physics.Arcade.SpriteWithDynamicBody;

    constructor() {
            key: SCENEKEY_MAINMENU
    create() {
        this.Baloon = this.physics.add.sprite(200, 300, IMAGEASSET_BALOON_BLUE);
        this.Baloon.setOrigin(0.5, 0.5);
        this.Baloon.body.setGravity(0, 300);
        this.Baloon.on('pointerdown', this.onBaloonClick, this);

    onBaloonClick(pointer: Phaser.Input.Pointer, localX: number, localY: number) {
        (this.Baloon.body as Phaser.Physics.Arcade.Body).allowGravity = false;

As you would notice, body.stop(), and body.allowGravity = false both statements are needed in order to completely stop the object from moving.

Alternatively, depending on your situation, you may change velocity, gravity and acceleration values in order to completely stop the object from moving.



Leave a Reply

Your email address will not be published. Required fields are marked *