Need help with a basic scene manager

Hi

I am very new to Haxe and I am trying to create a simple scene manager for openfl.
I have created a gist of what I have tried.

I am struggling with understanding how can I dynamically create the scenes and initialize their classses.

Here’s three ways you could do instances:

1.) Use a string name and initialize the scenes in advance

public static var scenes = new Map<String, Scene>();

...

scenes["title"] = new TitleScene();
scenes["logo"] = new LogoScene();

...

function showScene(sceneName:String):Void
{
    currentScene = scenes[sceneName];
    ...
}

2.) You could use the class constructors as an argument and call them directly

showScene(TitleScene.new);

...

function showScene(constructor:Void->Void)
{
    currentScene = constructor();
    ...
}

3.) You could go the dynamic route and dynamically instantiate the class

showScene(TitleScene);

...

function showScene(scene:Class<Dynamic>):Void
{
    currentScene = Type.createInstance(scene, []);
    ...
}

https://api.haxe.org/Type.html#createInstance

4.) You could even just pass an instance

showScene(new IntroScene());

...

function showScene(scene:Scene):Void
{
    currentScene = scene;
    ...
}

Personally I’ve used the string approach the most so that I’m not creating/destroying instances to translate through scenes but otherwise I would probably pass an instance rather than the class. That gives you more flexibility to re-use instances or to have constructor arguments if you want.

There’s no one way to do it :slight_smile:

Thanks. This is very helpful!