Jump to content

Passing Phaser's game object to a class


Recommended Posts

let MainMenu = new Phaser.Class({
    Extends: Phaser.Scene,

        function MyScene(config) {
            Phaser.Scene.call(this, config)

    preload: function () {
        this.load.image('Logo', 'assets/Logo.png');


    create: function () {
        this.Logo = this.add.image(1024/2 ,100, 'Logo').setScale(2);

I have this class (Scene?) that should represent my game's main menu. But, i want to pass to this class the game object that i'm creating in another file. In this case i want it merely to set my game's logo in top center using game.width and game.height as a way to make the logo be correctly placed with any size of the canvas. How can i do it?

Link to comment
Share on other sites

In terms of passing an object you just make it the second parameter when you call the scene.


this.scene.launch('MainMenu', passedObject);


Then in the init function of receiving class you add the parameter to receive.

init(data) {
    recievedObject = data;


Although for what you are looking to do you won't have to pass the object as game is accessible through the scenes system:



It might just be easiest to use the width and height of the camera so:

this.Logo = this.add.image(this.cameras.main.width / 2 ,100, 'Logo').setScale(2);


Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...