I just got it to work
First, you should be able to remove “pixijs”, “openfl-tools” and “hxcpp” from the project.xml
Second, we can’t override Flash’s loaderInfo
events, unfortunately. As a result, you’ll need to use addEventListener
instead of loaderInfo.addEventListener
in your preloader to get our events. Similarly, you should use event.bytesLoaded
and bytesTotal
from the ProgressEvent
rather than checking loaderInfo
again
Last, try .defaultTextFormat = fmt
instead of setTextFormat
, you’ll need to use setTextFormat
repeatedly after changing the text in order for it to work properly in Flash
Here’s my changes to Preloader.hx
package;
import openfl.display.Sprite;
import openfl.display.DisplayObject;
import openfl.events.*;
import openfl.text.Font;
import openfl.text.TextField;
import openfl.text.TextFormat;
import openfl.text.TextFormatAlign;
import openfl.text.TextFieldType;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.PixelSnapping;
import openfl.geom.Point;
import openfl.geom.Matrix;
import openfl.system.Capabilities;
import openfl.Lib;
@:bitmap("Data/Preload.png") class Splash extends BitmapData {}
@:font("Data/Open Sans.ttf") class DefaultFont extends flash.text.Font {}
@:keep class Preloader extends Sprite {
private var progress:Sprite;
private var textPercent:TextField;
private var splashImage:Bitmap;
public function new () {
super();
preloaderInit();
}
public function preloaderInit():Void {
// disable default menus
#if (flash || js)
Lib.current.stage.showDefaultContextMenu = false;
#end
#if (flash)
Lib.current.stage.addEventListener(MouseEvent.RIGHT_CLICK, function(e:Event){ });
if (Capabilities.playerType == "StandAlone" || Capabilities.playerType == "External") {
Lib.fscommand("showmenu", "false");
Lib.fscommand("trapallkeys", "true");
}
#end
Font.registerFont(DefaultFont);
Lib.current.stage.color = 0xFAFAFA;
Lib.current.stage.frameRate = 60;
// splash image
var splashWidth:Float = 128;
var splashHeight:Float = 128;
var splashOffsetY:Float = -128;
splashImage = new Bitmap(new Splash(Std.int(splashWidth), Std.int(splashHeight)), PixelSnapping.AUTO, true);
splashImage.x = (Lib.current.stage.stageWidth/2)-splashWidth/2;
splashImage.y = ((Lib.current.stage.stageHeight/2)-splashHeight/2) + splashOffsetY;
addChild(splashImage);
// progress bar
progress = new Sprite();
progress.scaleX = 0.0;
progress.graphics.beginFill (0x29B1FF);
progress.graphics.drawRect(620, 5, 670, 5);
addChild(progress);
// progress info text
textPercent = new TextField();
var textWidth:Float = 128;
var textHeight:Float = 128;
var tFormat:TextFormat = new TextFormat();
tFormat.font = "Open Sans";
tFormat.color = 0xAA0000;
tFormat.size = 24;
tFormat.align = TextFormatAlign.CENTER;
textPercent.defaultTextFormat = tFormat;
textPercent.x = (Lib.current.stage.stageWidth/2)-textWidth/2;
textPercent.y = (Lib.current.stage.stageHeight/2)-textHeight/2;
textPercent.width = textWidth;
textPercent.height = textHeight;
textPercent.textColor = 0x000000;
textPercent.backgroundColor = 0x000000;
textPercent.text = "";
textPercent.selectable = false;
textPercent.embedFonts = true;
addChild(textPercent);
// loading events
addEventListener(Event.COMPLETE, loaderInfo_onComplete);
addEventListener(ProgressEvent.PROGRESS, loaderInfo_onUpdate);
trace("LOADING START!");
}
// to test:
/*
public function scaleAroundPoint(target:DisplayObject, point:Point, scaleFactor:Float):Void
{
var m:Matrix = target.transform.matrix;
m.translate( -point.x, -point.y );
m.scale( scaleFactor, scaleFactor);
m.translate( point.x, point.y );
target.transform.matrix = m;
}*/
public function loaderInfo_onComplete(e:Event):Void {
trace("LOADING COMPLETE!");
};
public function loaderInfo_onUpdate(e:ProgressEvent):Void {
var _bytesTotal:Float = e.bytesTotal;
var _bytesLoaded:Float = e.bytesLoaded;
trace ("LOADING PROGRESS: " + _bytesLoaded + "/" + _bytesTotal + " bytes");
var _loadedPercent:Float = _bytesLoaded/_bytesTotal*100;
if (_bytesTotal == 0) {
progress.scaleX = 0;
} else {
progress.scaleX = (_loadedPercent/100); // needs to scale from center
//trace(_loadedPercent/100);
textPercent.text = "Loading...\n"+Std.string( Std.int(_loadedPercent) )+"%";
//scaleAroundPoint(progress, new Point(64, 64), _loadedPercent);
}
}
// probably needs onResize re-adjustments as well
// ...
}
EDIT: You can use openfl test flash -Dsimulate-preloader
on the command-line, or if you wish to hard-code it, <haxeflag />
works, but <haxedef name="simulate-preloader" />
or <haxedef name="simulate-preloader" value="5000" />
(or whatever value you want) might feel a little cleaner
Thanks for sharing your code! Made it a lot easier to debug