I tried again, but the progress event was indeed not executed!
Even simulated downloads cannot be executed
Loader progress does not execute URLLoader progress execution
When targeting html5, Loader
uses the JavaScript type HTMLImageElement
to load images. On other targets, it uses an internal URLLoader
to load the binary data and convert it into a bitmap after loading. I’m not sure why it was chosen to be implemented differently in html5, but that seems to be why it is not dispatching ProgressEvent.PROGRESS
.
Based on some quick research, it appears that HTMLImageElement
does not dispatch its own “progress” event. With that in mind, there is nothing for OpenFL to listen for, and that’s why it is not dispatching ProgressEvent.PROGRESS
.
So how did everyone do it? Should I use URLLoader to load it?
How did everyone do it?
Should I use URLLoader to load it? I need a solution
<source path="Source" />
<haxelib name="openfl" />
<assets path="fonts" include="*" embed="true" />
<assets path="images" include="*" embed="false" />
<assets path="zoe" include="*" exclude="*.fla|*.swf" embed="false" />
<assets path="sounds" include="*" embed="false" />
<window allow-high-dpi="true" />
package;
import openfl.media.Sound;
import haxe.Json;
import openfl.display.BitmapData;
import openfl.utils.Future;
import openfl.utils.ByteArray;
import openfl.net.URLRequest;
import openfl.events.Event;
import openfl.events.ProgressEvent;
import openfl.net.URLLoaderDataFormat;
import openfl.utils.Function;
import openfl.net.URLLoader;
import openfl.display.Sprite;
class Queue extends Sprite {
private var loader:URLLoader;
private var id:String;
private var callProgress:Function;
private var callComplete:Function;
public function load(url:String, type:String, progress:Function = null, complete:Function = null):Void {
id = type;
callProgress = progress;
callComplete = complete;
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(ProgressEvent.PROGRESS, progressFn);
loader.addEventListener(Event.COMPLETE, completeFn);
loader.load(new URLRequest(url));
}
private function progressFn(e:ProgressEvent):Void {
callProgress != null ? callProgress(e, id) : 0;
}
private function completeFn(e:Event):Void {
loader.removeEventListener(ProgressEvent.PROGRESS, progressFn);
loader.removeEventListener(Event.COMPLETE, completeFn);
callComplete != null ? callComplete(e, id) : 0;
}
public function new() {
super();
}
public static function getBit(e:Event, complete:Function):Void {
var loader:URLLoader = cast(e.currentTarget, URLLoader);
var gg:Future<BitmapData> = BitmapData.loadFromBytes(loader.data);
gg.onComplete(function(e:BitmapData):Void {
complete(e);
loader = null;
gg = null;
});
}
public static function getJson(e:Event):Dynamic {
return Json.parse(cast(e.currentTarget, URLLoader).data);
}
public static function getSound(e:Event):Sound {
var byte:ByteArray = cast(e.currentTarget, URLLoader).data;
var sound:Sound = new Sound();
sound.loadCompressedDataFromByteArray(byte, byte.length);
return sound;
}
}