1.21 gigawatts
1.21 gigawatts

Reputation: 17716

How to use a SWF as a Flex 4 preloader?

I'm sure this has been asked before but I can't find it. I'd like to show a SWF movieclip in a Flex preloader.

I've extended SparkDownloadProgressBar and all I'm getting is a white screen and then eventually the app loads.

Here is what I have so far:

package 
{
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.events.ProgressEvent;

    import mx.preloaders.SparkDownloadProgressBar;

    public class MyPreloader extends SparkDownloadProgressBar
    {
        public function MyPreloader()
        {
            //backgroundImage = PreloaderGfx;
            //backgroundImage = "butterfly.swf";
            //backgroundColor = 0xFF0000;
            //visible = true;
        }

        //[Embed(source="/runninghorses.swf", symbol="PreloaderGfx")]
        [Embed(source="/butterfly.swf")]
        private var PreloaderGfx:Class;


        override public function initialize():void {
            super.initialize();
            createChildren();
        }

        override protected function createChildren():void {
            //throw new Error();
            super.createChildren();
            var displayObject:DisplayObject = getChildAt(0);

            trace("d width:" + displayObject.width);
            trace("d height:" + displayObject.height);

            if (displayObject is PreloaderGfx) {
                MovieClip(displayObject).play();
            }

        }

        override protected function showDisplayForDownloading(elapsedTime:int, event:ProgressEvent):Boolean {
                    trace("showDisplayForDownloading: elapsedTime = " + elapsedTime);
                    trace("showDisplayForDownloading:" + event.bytesLoaded + " " +  event.bytesTotal + " " +  (event.bytesLoaded < event.bytesTotal / 2));
            return elapsedTime > 100 &&
                event.bytesLoaded < event.bytesTotal / 2;
        }

    }
}

Upvotes: 0

Views: 30

Answers (1)

Philarmon
Philarmon

Reputation: 1806

First, don't call createChildren() in the initialize() method. createChildren() will be called by the Flex framework automatically, no need to call it twice and it's not how it is supposed to work anyways. In your case just remove your overridden initialize method completely.

Your main problem is that you have only embedded the swf but it's not on stage yet. Embed will just compile your asset to your swf but won't display it. You will need to add it yourself in the createChildren(). And you would need to wrap your swf into some kind of UIComponent so you can add it to stage. An Image might be the easiest solution (untested, just to give you an idea):

override protected function createChildren():void 
{
    super.createChildren();

    var img:Image = new Image();
    img.source = PreloaderGfx;
    addChild(img);
}

Upvotes: 0

Related Questions