简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM