1

I need to launch the phaser game with some input data such as: user, authorization token,score etc. How can I proceed. thx

I didn't figure out how to do it

this is my index.js

import Phaser
import BootScene from './scenes/boot'
import PreloadScene from './scenes/preload'
import GameScene from './scenes/game'
import GameScalePlugin from 'phaser-plugin-game-scale'
import {config} from './config'

let phaserConfig = config.phaser
phaserConfig.type = Phaser.AUTO
//phaserConfig.scene = [PreloadScene, GameScene]
phaserConfig.plugins = {
global: [{
    key: 'GameScalePlugin',
    plugin: GameScalePlugin,
    mapping: 'gameScale',
    data: {
        debounce: false,
        debounceDelay: 50,   // Debounce interval, in ms
        maxHeight: Infinity,
        maxWidth: Infinity,
        minHeight: config.size.minHeight,
        minWidth: config.size.minWidth,
        mode: 'fit',
        resizeCameras: false, // Resize each scene camera when resizing the game
        snap: null, 
       }
    }]
   }
   phaserConfig.banner = { hidePhaser: true }
   let game = new Phaser.Game(phaserConfig)
   game.scene.add('BootScene', BootScene, true, {user:'test', auth:'test-auth'});
   game.scene.add('PreloadScene', PreloadScene);
   game.scene.add('GameScene', GameScene);

and this my boot.js

import { Scene } from 'phaser'
export default class BootScene extends Phaser.Scene {
constructor() { super({ key: 'Boot' }) }

    preload() {
        this.load.image('logo', './assets/logo.png')
    }

    init(...params){
        console.info('INIT', params)
    }

    create(...params){   
        this.scene.start('Preload')
    }
}

But in the boot it receives an empty object. is a problem within my config file?

winner_joiner
  • 12,173
  • 4
  • 36
  • 61
Diego
  • 37
  • 5

1 Answers1

0

There are many ways a easy one is simply using the init function of the scene. By starting the scene with the function add on the SceneManager you can pass parameters to the scene. (link to the documentation)

Here a short demo:

document.body.style = 'margin:0;';

var config = {
    type: Phaser.AUTO,
    width: 536,
    height: 18,
    backgroundColor: 0xffffff,
    banner: false,
}; 

class TestScene extends Phaser.Scene {
    constructor(){
      super('TestScene')
    }
    init(...params){
        console.info('INIT', JSON.stringify(params))
    }
    create (...params){
        console.info('CREATE', JSON.stringify(params))
    }
}

let game = new Phaser.Game(config);

game.scene.add('TestScene', TestScene, true, {user:'test', auth:'test-auth'});

setTimeout( () => {
    console.info(' second Game, with not matching keys');
    
    let game2 = new Phaser.Game(config);

    game2.scene.add('NotMatching', TestScene, true, {user:'test', auth:'test-auth'});
}, 10);
<script src="//cdn.jsdelivr.net/npm/phaser/dist/phaser.min.js"></script>

Info: the passed parameters will be passed to the init and the create function, of the started scene. I personally try to always use init when possible.

winner_joiner
  • 12,173
  • 4
  • 36
  • 61
  • I updated my question with the index and boot file – Diego Apr 18 '23 at 10:51
  • You didn't aska a question, but I assume you want to knwo Why it doesn't work. The _keys_ have to match. set the key in the constructor to "BootScene". I will update may answer to show the problem. – winner_joiner Apr 18 '23 at 11:00