Here my solution for anybody who have the same problem, i think it can be improved but it works^^
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.utils.ByteArray;
import flash.utils.getDefinitionByName;
/**
* ...
* @author AuthorName
*/
public class Preloader extends MovieClip
{
private static const FRAMENUM:Number = 120;
[Embed(source = "../assets/Movies/loading.swf", mimeType = "application/octet-stream")] private var PRELOADER_ANIM:Class;
private var mc: MovieClip;
private var percent:Number = 0;
private var ldr: Loader = new Loader();
public function Preloader()
{
if (stage) {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
}
ldr.contentLoaderInfo.addEventListener( Event.COMPLETE, onLoad );
ldr.loadBytes(new PRELOADER_ANIM as ByteArray);
}
public function onLoad( e:Event ):void
{
if( !e.target )
return;
mc = e.target.content as MovieClip;
addChild(mc);
addEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
}
private function ioError(e:IOErrorEvent):void
{
trace(e.text);
}
private function progress(e:ProgressEvent):void
{
if (mc != null)
{
percent = Math.round(e.bytesLoaded / e.bytesTotal * 100);
mc.gotoAndStop( (int)(percent * FRAMENUM / 100) );
}
}
private function checkFrame(e:Event):void
{
if (currentFrame == totalFrames)
{
stop();
loadingFinished();
}
}
private function loadingFinished():void
{
removeEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
startup();
}
private function startup():void
{
var mainClass:Class = getDefinitionByName("Main") as Class;
if (parent == stage) stage.addChildAt(new mainClass() as DisplayObject, 0);
else addChildAt(new mainClass() as DisplayObject, 0);
parent.removeChild(this);
}
}
Thank you again!^^