I know this is a trodden thing to say, but I’m new to haxe and Openfl,and I was wondering if there is an easy way to switch screens.
Unfortunately I couldn’t find any tutorial for haxe, so, do you know a good tutorial or is there a build-in function in Openfl to switch between screens?.. or do I have to write my own screen manager?
One possible way is to make a class for each screen:
class MenuScreen extends Sprite {
super();
addChild(new Bitmap(Assets.getBitmapData("Title.png")));
//Add buttons and so on...
}
class GameScreen extends Sprite {
var playerCharacter:Character;
public function new() {
super();
playerCharacter = new Character();
addChild(playerCharacter);
//Etc.
}
}
Then you could switch between them:
public static function toGame():Void {
Lib.current.removeChild(currentScreen);
currentScreen = new GameScreen();
Lib.current.addChild(currentScreen);
}
public static function toMenu():Void {
Lib.current.removeChild(currentScreen);
currentScreen = new MenuScreen();
Lib.current.addChild(currentScreen);
}
HaxeFlixel does something like this; look at their examples for more.
How to use. In Main class you need to initialize manager:
ScreenManager.initialize( stage ); // stage or any DisplayObject, it will be a container for all screens
Then create the screen. Basic screen class lloks like:
class GameScreen extends Sprite implements IScreen
{
public function new() {
super();
}
public function initialize():Void {
// here is your init code
}
public function update(deltaTime:Float):Void {
// here is your enterframe function
}
public function unload():Void {
// and this function will be called when you change the screen.
}
}
Whatever you do, you might consider supporting a “hide” and “show” for scenes, which may take time. This is incredibly useful for animating a scene out, and the next one in, even if it is just a simple fade, rather than assuming it will be an instant transition.
I also tend to use a showScene () method, something like this:
This was an over-simplified example, but it uses a currentScene value to know what scene you are coming from, depending, it can make the previous scene disappear properly. You could wrap this into a hide () method if you use a common base class. Similarly, it switches on the new requested scene, which could be put in a show () method, to show the next scene. Another optimization might be to return if (currentScene == scene) so that you can call showScene repeatedly without restarting the animation.
If you want “third-party” solutions, HaxeFlixel does this out-of-the-box. You organize your code into a series of state classes, and switch between them (one line of code).
Hi , i have a question i used something like this to change of scenes this work in flash, android and html5 but neko only the first time i call the function but i cant go to any scenes… can you help me?