Problem with preload / create level classes?

So is there any reason why my scene manager class is not switching scenes from the ‘preloading-scene’ class to the ‘create-scene’ class then to the ‘lvl-1’ class?

Here’s what it’s doing :

It should alert the user that it’s Inside Create Scene in create ( ) function going to key :: lvl-1

and finally, it should tell the user :

Inside scene key :: 'lvl-1'...

level-manager.js :

class LevelManager extends Phaser.Scene {

	constructor ( __objData ) {

		super ( __objData );

		this.__level = 0;
		this.__objData = __objData;
		this.__scene = this.__objData.scene;

		this.__data = [
			this.__scene, 
		];

	}

	nextLevel ( __scene, __sceneKey ) {

		this.__level++;

		this.__scene = __scene;
		this.__sceneKey = __sceneKey;

		console.error ( this.__level );
		console.error ( this.__scene );
		console.error ( this.__sceneKey );

		alert ( 'Inside `Level Manager` switching to scene key' + ' :: ' + '`' + this.__sceneKey + '`' + '...' );

		this.__scene.scene.start (
			this.__sceneKey, 
			this.__data [ this.__level ]
		);

		if ( this.__level ) {
			console.log ( 'SCENE' + ' :: ' + ' { ' + '\'' + ( this.__level ) + '\'' + ' } ' + 'WITH' + ' ' + 'SCENE KEY' + ' ' + '::' + ' { ' + '\'' + this.__sceneKey + '\'' + ' } ' + 'HAS BEEN STARTED!' );
			console.log ( 'Data :: ' + '\r\n', this.__data [ this.__level ] );
		}

	}

}

preloading-scene.js :

/**
 * A class that extends Phaser.Scene & wraps up 
 * the core logic for the preloader
 */

class PreloadingScene extends Phaser.Scene

{

	constructor ( ) {
		super ({ 
			key : 'preloader', 
		});
	}

	__LoadAllAssets ( ) {
	}

	preload ( ) {
		alert ( 'Inside Preloader Scene in preload ( ) function' + '...' );
		this.__LoadAllAssets ( );
	}

	init ( ) {
		alert ( 'Inside Preloader Scene in init ( ) function' + '...' );
	}

	create ( ) {

		alert ( 'Inside Preloader Scene in create ( ) function' + '...' );

		this.__level = new LevelManager ({
			scene : this, 
		});

		alert ( 'Inside Preloader Scene going to key' + ' :: ' + '`' + 'create-scene' + '`' + '...' );

		this.__level.nextLevel ( this, 'create-scene' );

	}

}

create-scene.js :


/**
 * A class that extends Phaser.Scene & wraps up 
 * the core logic for the create scene
 */

class CreateScene extends Phaser.Scene {

	constructor ( ) {
		super ({
			key : 'create-scene', 
		});
	}

	create ( ) {
		alert ( 'Inside Create Scene in create ( ) function going to key :: lvl-1' );
		this.__level.nextLevel ( this, 'lvl-1' );
	}

}

lvl-1.js :

/**
 * A class that extends Phaser.Scene & wraps up 
 * the core logic for the level
 */

class Level1 extends Phaser.Scene {

	constructor ( ) {
		super ({
			key : 'lvl-1', 
		});
	}

	preload ( ) {

	}

	init ( ) {

		// Set whether or not the Player is dead { Default :: false }

		this.__isPlayerDead = false;

		// Set whether or not the Player fell into a hole { Default :: false }

		this.__playerFallsInHole = false;

		// Set whether or not the Player has overlapped the Goal

		this.__hasOverlapped = false;

	}

	create ( ) {

	}

	update ( ) {

		stats.update ( );

		if ( ( ! ( this.__playerFallsInHole ) ) && ( this.hero.sprite.y > this.__groundLayer.height ) )

		{

			// Flag that the player has fallen into hole so that we can 
			// stop update from running in the future

			this.KillPlayer ({
				scene : this.scene, 
				entity : this.hero, 
			});

			this.__playerFallsInHole = true;

		}

		this.hero.stateMachine.step ( );

	}

}

Any help is greatly appreciated!

1 Like

I think you have add import and export statement for each class as like

export default LevelManager 
````

@julianos : I do not use typescript. I use ES6.

1 Like

Wel ,you use phaser 3 or not?

@julianos : Yes, I use Senku - Version 3.21.0.

1 Like

Take a look at this i hit the some problem and i use the some phaser version

@julianos : I do not understand this post. Did you take a look at the code I posted?

1 Like

How you link html file?

@julianos : O, you wish to see my index file? ok, here it is :

<!DOCTYPE html>

<html>

	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, minimum-scale=1, user-scalable=no">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">

	<head>

		<title>Scene Switcher</title>
		<link rel="stylesheet" href="res/src/css/main.css">

	</head>

	<body oncontextmenu="return false;" oncut="return false;" oncopy="return false;" onpaste="return false;">

		<div id="game-container"></div>

		<script src="res/src/js/libs/modules/stats/stats.min.js"></script>
		<script src="res/src/js/libs/modules/phaser/3.21.0/senku/build/phaser.js"></script>
		<script src="res/src/js/fsm.js"></script>
		<script src="res/src/js/constants.js"></script>
		<script src="res/src/js/vars.js"></script>
		<script src="res/src/js/keys.js"></script>
		<script src="res/src/js/animation-functions.js"></script>
		<script src="assets/objects/scripts/objects.js"></script>
		<script src="res/src/js/game-data.js"></script>
		<script src="res/src/js/level-manager.js"></script>
		<script src="res/src/js/lvl-1.js"></script>
		<script src="res/src/js/lvl-2.js"></script>
		<script src="res/src/js/preloading-scene.js"></script>
		<script src="res/src/js/create-scene.js"></script>
		<script src="res/src/js/main.js"></script>

	</body>

</html>
1 Like

Why do you use all this resources files in html???

@julianos : It’s easier for me to do it that way. I don’t really like typescript that well. But I do love ES6.

Well i use Es6 also but i use one linked html files and one as phaser.io resources.Watch this video if you want
https://www.youtube.com/watch?v=_3oSWwapPKQ&t=182s

I also like ES6 and made a full working example of classes, plus some other stuff.

Hey @EddieOne. I have checked tour example above. Just curious. I see you have used the es6 import and export in browser.
But why did you not link the entry point javascript file index.js to the index.html file?

Hello, It is because Babel is used to give ES6 on the client side too. What happens behind the scenes, server.js will compile index.html and bundle all the client js into one package, adding a line to load it automatically.

Oh yes. I am used to this things very much indeed just in non game development. I could not guess here. Thanks.

Guys, I already have this issued fixed, thank you! :slight_smile:

Great! Can you post the solution as well or mark the one if already posted?

Well it’s hard to explain, but here’s the basics :

'TitleScene.js' :

var TitleScene = new Phaser.Class

({

	Extends : Phaser.Scene, 

	initialize : 

	function TitleScene ( ) {

		Phaser.Scene.call ( this, {

			key : 'TitleScene', 
			active : false, 

		} );

	}, 

        myFunc : function ( ... ) {

        }

});

this.__titleScene = new TitleScene ( );

this.__titleScene.myFunc ( ... );

Hope that helps! :slight_smile:

No explanation at all? Sad.

I am stuggling with having a game system like a menu, menu bar and just under it a game playground. Clicking settings, pause etc in menu bar opens a modal like in web and. Playground background becomes darkened. Also there are other pages than playground like achievements view, player selection corousel etc.

I am an exprerinced JavaScript full stack developer developed with Node.js React Redux etc but never games. I worked with Three.js and Babylonjs as well but not with game engine or projects.

Do for those listed tasks I have many ideas but I am not sure which way would be better and not a bad design not an abuse. So it would be helpful to see similar wuestions and answers. I am reading devlogs 119 and 121 to solve those.

Thanks