Flex custom preloader does not center


All our projects use the same preloader. Like many people, we based our preloader on Jesse Warden’s tutorial.

Recently, it started doing funny by not centering. Google-ing around gave some hints, but none of them worked for me. Here is my solution.

Hide the preloader (line 6):


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

Use the ’stage’ property (see blog jaap kooiker). And unhide the preloader if we get a valid value for the stage width:

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;
}
}

Center the preloader on each progress event:

private function onSWFDownloadProgress( event:ProgressEvent ):void
{
centerPreloader();
updateProgressBar(event.bytesLoaded/event.bytesTotal);
}
  1. #1 by Tink - April 14th, 2010 at 09:32

    You could override the public methods

    public function set stageWidth(value:Number):void
    public function set stageHeight(value:Number):void

    and center you proloader using these values (or just use the getters).

(will not be published)
  1. No trackbacks yet.