How to change gravity just in one scene (Phaser)?

Viewed 334

I would like to learn making games using Phaser.

I know you define the gravity in the config, like this:

var config = {
        type: Phaser.AUTO,
        width: 800,
        height: 600,
        physics: {
            default: 'arcade',
            arcade: {
                gravity: { y: 300 },
                debug: false
            }
        },
        scene: [FirstScene, SecondScene,...]
    };

But this applies to all scenes. Is there a way how to change the gravity in just one scene.

1 Answers

You can specify the gravity for a particular scene by redefining the physics settings in the scene's constructor method.

class FirstScene extends Phaser.Scene {
  constructor() {
    super({
      key: 'first',
      physics: {
        default: 'arcade',
        arcade: { 
          gravity: { y: 2000 }
        }
      }
    });
} 

Check out the API Docs to see what else can be configured on a scene-by-scene basis.

Related