Hi,
I’m setting up a Preloader on my openFL game, it works fine, but i would like, when the loader has complete, to delay the moment when the loader will be hidden and the main will be added to the stage, in order to play a nice tween to remove to loader (rather then suddenly seeing it disappear)
On my project.xml, i added
<app main="Main" file="xxx" path="bin" preloader="CPreloader" />
I’m using the following CPreloader class :
package;
import com.vinc.layout.LayoutManager;
import openfl.Assets;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.PixelSnapping;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.events.ProgressEvent;
import openfl.geom.Point;
@:keep class CPreloader extends Sprite {
public function new () {
super ();
// loading events
addEventListener(Event.ADDED_TO_STAGE, onInit);
addEventListener(Event.COMPLETE, loaderInfo_onComplete);
addEventListener(ProgressEvent.PROGRESS, loaderInfo_onUpdate);
}
public function onInit (e:Dynamic = null):Void {
trace ("Preloader.init");
initAssets();
}
public function loaderInfo_onComplete(e:Event):Void {
trace("LOADING COMPLETE!");
};
public function onLoaded (e:Dynamic = null):Void {
trace ("Preloader.loaded");
var delay = 60;
addEventListener (Event.ENTER_FRAME, function (_) {
delay--;
if (delay == 0) {
trace ("delayed start");
//dispatchEvent (new Event (Event.COMPLETE));
}
});
}
public function loaderInfo_onUpdate(e:ProgressEvent):Void {
var bytesTotal:Float = e.bytesTotal;
var bytesLoaded:Float = e.bytesLoaded;
var _progress:Float = bytesLoaded / bytesTotal;
if (bytesTotal == 0) {
setAssetProgress(0);
} else {
setAssetProgress(_progress);
}
}
//_____________________________________________________________________
function initAssets()
{
var bg:Bitmap = createBitmap(new BackgroundBMPD(750, 1334));
this.stage.addChildAt(bg, 0);
bg.name = "bgloader";
LayoutManager.init(null, new Point(Constants.GAME_WIDTH, Constants.GAME_HEIGHT), new Point(stage.stageWidth, stage.stageHeight));
LayoutManager.resizeContainer(bg, 1, null);
bg.x = stage.stageWidth * 0.5 - 750 * bg.scaleX * 0.5;
bg.y = stage.stageHeight * 0.5 - 1334 * bg.scaleY * 0.5;
var container:Sprite = new Sprite();
addChild(container);
var loader:Bitmap = createBitmap(new LoaderBMPD(366, 300));
container.addChild(loader);
loadersub = createBitmap(new LoaderSubBMPD(73, 18));
container.addChild(loadersub);
loadersub.x = 202; loadersub.y = 136;
_mask = new Sprite();
_mask.graphics.clear();
_mask.graphics.beginFill();
_mask.graphics.drawRect(0, 0, 73, 18);
container.addChild(_mask);
_mask.x = loadersub.x; _mask.y = loadersub.y;
loadersub.mask = _mask;
container.x = this.stage.stageWidth * 0.5 - 366 * 0.5 - 10;
container.y = this.stage.stageHeight * 0.5 - 300 * 0.5;
}
private function setAssetProgress(_progress:Float):Void
{
var step:Int = Math.floor((_progress * 100) / 8);
var step2:Int = step % 4;
_mask.scaleX = step2 / 3;
}
private function createBitmap(_bmpd:BitmapData):Bitmap
{
return new Bitmap(_bmpd, PixelSnapping.AUTO, true);
}
}
@:bitmap("assets/preload/bg.png") class BackgroundBMPD extends BitmapData {}
@:bitmap("assets/preload/asset-loader.png") class LoaderBMPD extends BitmapData {}
@:bitmap("assets/preload/asset-loader-point.png") class LoaderSubBMPD extends BitmapData {}
Thanks in advance