Flex 4 ~ set preloader center

問題是說設定preloader置中卻得到null的stageWidth

因為場景還未建立完成~

這邊有solution

先hide preloader

public override function set preloader(p:Sprite):void
{
_preloader = p;
p.addEventListener( ProgressEvent.PROGRESS , onSWFDownloadProgress );
p.addEventListener( FlexEvent.INIT_COMPLETE , onFlexInitComplete);
p.visible = false;
}


然後讓她每跑個進度讓他置中

private function onSWFDownloadProgress( event:ProgressEvent ):void
{
centerPreloader();
//updateProgressBar(event.bytesLoaded/event.bytesTotal);
}

在有抓到stageWidth數值的時候置中並設定visible

private function centerPreloader():void
{
if ((stage.stageWidth > 0) && (_preloader)) {
x = (stage.stageWidth / 2) - (clip.width / 2);
y = (stage.stageHeight / 2) - (clip.height / 2);
_preloader.visible = true;
}
}


source
http://tjoadesign.nl/blog/?p=83

沒有留言: