Floating in Phaser 3

If you are using balloons in your game, then you’ll want to make them float. To use floating in Phaser 3, you simply need to use a negative value for the Y property of gravity. To make an object float diagonally simple set the X property.

Check out this code sample to see what I mean.

class SceneMain extends Phaser.Scene {
    constructor() {
        super('SceneMain');
    }
    preload()
    {
    	this.load.image("balloon","images/balloon.png");
    }
    create() {
        //add a sprite to physics 
        this.balloon=this.physics.add.sprite(240,480,"balloon");

        //set the origin of the balloon to the center
        this.balloon.setOrigin(0.5,0.5);

        //set the gravity on the sprite's physics body
        this.balloon.body.setGravity(0,-25);

    }
    update() {}
}

Leave a Comment