The ScreenManager.as script, with the Screen.as script will help you to have a clear flow between screens.
Note: This script needs a public static instance inside Main.as of Mono.as and had already instanced UpdateManager.as
Initial set up ScreenManager.as
In order to use it, first, you will need a public static instance of Mono.as in your Main class. Then, you will need to create an instance of UpdateManager.as inside Mono.as. Once you have it, you will be able to use it without any problem. You can create a new var with an instance of ScreenManager.as or you can just the one that is inside Mono and will let you have access from anywhere. For example, in case you want to do the second option (recommended), you should do the following:
package { import Mono.Managers.ScreenManager; import Mono.Managers.UpdateManager; import Mono.Mono; import flash.display.Sprite; import flash.utils.Dictionary; public class Main extends Sprite { public static var mono:Mono; public function Main() { mono = new Mono(stage); mono.updateManager = new UpdateManager(); mono.screenManager = new ScreenManager(); } } }
Now you will be able to access it from anywhere by accessing mono first.
Creating Screens
For each screen you want to create, you will need to create a new class that inherits from Screen.as class. Then you will be able to override functions and use it with the ScreenManager. For example, you can create the class Menu that will inherit from Screen:
package { import Mono.Managers.Complements.Screen; public class Menu extends Screen { public function Menu(name:String) { super(name); //Initiliaze the screen } public override function update():void { //Update the screen } public override function exit():void { //Close the screen } } }
Then, as the comments say, in the constructor class you will be able to initialize the class. Then, the update will be called once per frame and for last the exit will be called when the screen is changed.
Registering screens
In order to start using the screens with the ScreenManager.as, you will have to register each screen. To register them, you will only need to have the class you want to register (that inherits from Screen) and a name to assign it as the index.
For example, we can register the last screen under the name of “The menu screen” by doing the following:
Main.mono.screenManager.registerScreen("The menu screen", Menu);
This will work from anywhere.
Changing screens
If you want to change to another screen. You will only need to say which one (the name of the screen will be the one that you used when registering) and call this method:
Main.mono.screenManager.loadScreen("The name of the screen to load");
The screen manager will change them by calling the needed methods.
Public Functions
- registerScreen(name:String, scr:Class):void
- loadScreen(name:String):void
- closeScreen():void
Private Functions
- evUpdate():void
- evChange(s:ScreenEvent):void
Private Vars
- _screens:Dictionary
- _currentScreen:Screen