skip to Main Content

phaser 3:

Uncaught TypeError: this.game.scene.launch is not a function unable to launch

I have implemented a scene manager, and I’m having trouble.

Please help. I really want to be able to use this scenemanager as it makes stuff a lot easier. I need to use launch because I am having a battle with a lot of temporary variables like the health bars and stuff, and when I open the inventory to use an item, I need to be able to resume the battle. It lets me use start, and pause, and resume, but when I start, it stops the previous scene. I needed to use a launch to get around this.

But its not letting me. .

The line I’m having trouble with is this line:

this.game.scene.launch(newSceneName, data);

The error I’m getting is:

Uncaught TypeError: this.game.scene.launch is not a function

I tried to provide context of this.game.scene.

I never used it like this before. Something wrong with the way I’m using it.

//when i start my game i run this.

const game = new Phaser.Game(config);


globals.initSceneManager(game);


game.scene.start('MainMenuScene');

//in my globals class i have this:

class Globals {
    constructor() {


        this._inputManager = new InputManager();
        this._sceneManager = null;
    }

    initSceneManager(game) {
        this.sceneManager = new SceneManager(game, this.inputManager); // Pass the input manager
        this.sceneManager.init(); // Initialize the SceneManager
    }

    //and getters and setters. 


    //this is my scenemanager. 


class SceneManager {
    constructor(game, inputManager) {
        this.game = game;
        this.inputManager = inputManager;
        this.currentScene = null;
        // this.sceneManager = game.scene;
    }


    init() {
        this.inputManager.init(this.game); // Initialize input manager with the game context
    }


    transitionTo(newSceneName, data = {}, pauseCurrent = false) {
        console.log(`Transitioning from ${this.currentScene} to ${newSceneName}`);
        console.log('SceneManager methods:', Object.keys(this.game.scene));
        console.log('Is start a function?', typeof this.game.scene.start === 'function');
        console.log('Is launch a function?', typeof this.game.scene.launch === 'function');
        // console.log('SceneManager methods:', Object.keys(this.sceneManager));


        // Disable input listeners immediately
        this.inputManager.disableListeners();


        try{
            if (this.currentScene) {
                if (pauseCurrent) {
                    // this.game.scene.pause(this.currentScene);
                    // console.log('Pausing: ', this.currentScene);
                    console.log('not stopping');
                } else {
                    this.game.scene.stop(this.currentScene);
                    console.log('Stopping: ', this.currentScene);
                }
            }
            // Set a short delay before starting the new scene
            setTimeout(() => {
                // this.currentScene = newSceneName;
                // this.game.scene.start(newSceneName, data);
                this.currentScene = newSceneName;

          
                if (pauseCurrent) {
                    console.log('Launching new scene:', newSceneName);
                    this.game.scene.launch(newSceneName, data);
                
                } else {
                    console.log('Starting new scene:', newSceneName);
                    this.game.scene.start(newSceneName, data);
                }

            
                // Set up new input listeners with a delay
                setTimeout(() => {
                    const inputs = this.getSceneInputs(newSceneName);
                    console.log('Setting inputs for', newSceneName, ':', inputs);
                    this.inputManager.setSceneInputs(newSceneName, inputs);            }, 50);
                }, 25);
            } catch (error) {
                console.error('Error during scene transition:', error);
            }
        }

2

Answers


  1. Your code seems a bit complex (it seems you are building your own framework, with phaser), nevertheless if you want to get the launch function, you could access it through one of the active Scenes

    this.game.scene.getScenes(true)[0].scene.launch(...);
    

    getScenes(true) gets all active Scenes (link to the documentation). But if you know the name of the scene you can use getScene(...) (link to the documentation).

    btw.: your code seems to have a bug in it, you are passing this.sceneManager = new SceneManager(game, this.inputManager);, but the property is called this._inputManager.

    Login or Signup to reply.
  2. There’s so much wrong with this code I’m not sure where to start. But, to answer your immediate question: launch is a method of the ScenePlugin, not the SceneManager. The Scene Manager is a global singleton that should only exist once and is owned by the Game instance. An instance of the Scene Plugin, on the other hand, is owned by each Scene and is the entry point at which you should be making Scene operations (start, stop, launch, etc) as it will queue them properly.

    Seeing you create your own instance of the global Input Manager is equally worrying. Again, this is a game level service that should exist once and belong to the game only. Each Scene has its own InputPlugin that is the point of access you should be using – and it doesn’t need instantiating directly either. In fact, doing so will just create 2 instances of them for every single Scene, which is highly redundant.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search