The NMEPreloader
class is deprecated until OpenFL 5 is released.
If you get this message, we have changed the preloader system in OpenFL with a focus on simplicity, and removing the requirement for extending a special type.
At first, we wanted to implement it over the LoaderInfo
object (similar to how Loader
works), but unfortunately, this approach was not possible on Flash Player, though maybe we’ll visit that as an alternative in the future.
To create a standard preloader, don’t do anything! OpenFL will create a preloader for you. To create a custom preloader, use a class that extends Sprite
, and specify the class name in your project.xml
<app preloader="MyPreloader" />
import openfl.display.Sprite;
class MyPreloader extends Sprite {
public function new () {
super ();
}
}
A simple preloader will show while things are loading, and be removed automatically when preloading is complete.
To listen to progress events, add a listener to ProgressEvent.PROGRESS
import openfl.display.Sprite;
import openfl.events.ProgressEvent;
class MyPreloader extends Sprite {
public function new () {
super ();
addEventListener (ProgressEvent.PROGRESS, onProgress);
}
private function onProgress (event:ProgressEvent):Void {
trace (event.bytesLoaded, event.bytesTotal);
}
}
You can listen for when preloading is complete using Event.COMPLETE
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.events.ProgressEvent;
class MyPreloader extends Sprite {
public function new () {
super ();
addEventListener (Event.COMPLETE, onComplete);
addEventListener (ProgressEvent.PROGRESS, onProgress);
}
private function onComplete (event:Event):Void {
trace ("complete");
}
private function onProgress (event:ProgressEvent):Void {
trace (event.bytesLoaded, event.bytesTotal);
}
}
After Event.COMPLETE
, the preloader is unloaded automatically, and the application is started. If you prefer, you can choose when the preloader is unloaded. In this case, preventDefault ()
on the COMPLETE
event, and dispatch Event.UNLOAD
when you done.
import haxe.Timer;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.events.ProgressEvent;
class MyPreloader extends Sprite {
public function new () {
super ();
addEventListener (Event.COMPLETE, onComplete);
addEventListener (ProgressEvent.PROGRESS, onProgress);
}
private function onComplete (event:Event):Void {
event.preventDefault ();
Timer.delay (function () {
dispatchEvent (new Event (Event.UNLOAD));
}, 2000);
}
private function onProgress (event:ProgressEvent):Void {
trace (event.bytesLoaded, event.bytesTotal);
}
}
You may choose to unload based upon an animation, or a “Start” button that the user presses, or any other logic