移相器 3:
Uncaught TypeError: this.game.scene.launch is not a function unable to launch
我已经实现了场景管理器,但遇到了麻烦。
请帮忙。我真的希望能够使用这个
scenemanager
,因为它使事情变得更加容易。我需要使用启动,因为我正在与许多临时变量(例如生命条之类的东西)进行战斗,当我打开库存以使用物品时,我需要能够恢复战斗。它允许我使用开始、暂停和恢复,但是当我开始时,它会停止前一个场景。我需要使用启动来解决这个问题。
但它不让我这么做。 .
我遇到问题的线路是这一行:
this.game.scene.launch(newSceneName, data);
我收到的错误是:
Uncaught TypeError: this.game.scene.launch is not a function
我试图提供
this.game.scene
的背景。
我以前从来没有这样用过。我的使用方式有问题。
//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);
}
}
你的代码看起来有点复杂(看起来你正在构建自己的框架,使用移相器),但是如果你想获得
launch
功能,你可以通过活动场景之一访问它
this.game.scene.getScenes(true)[0].scene.launch(...);
获取所有活动场景(链接到文档)。但如果您知道场景的名称,则可以使用getScenes(true)
(链接到文档)。getScene(...)
顺便说一句:你的代码似乎有一个错误,你正在传递
,但该属性被称为this.sceneManager = new SceneManager(game, this.inputManager);
。this._inputManager