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);
}
}
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 usegetScene(...)
(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 calledthis._inputManager
.