PDA

View Full Version : [Flex3] Custom Component initializes 3 times on Creation Complete??



tomaugerdotcom
August 4th, 2009, 05:45 PM
I have a custom AS3 component in a very simple MXML file. The custom component just loads an external SWF using SWFLoader.

If you look at the output (below) you'll see it's actually loading the SWF only once, but it instantiates it twice!! What the?

Main.mxml is just the Application tag and then this:

<myComponent:SlideViewer title="SlideViewer" swfName="slides/productInfo" />SlideViewer.as has this:

package com.unitron.localeEditor.view.component {
import flash.events.Event;
import flash.display.MovieClip;
import mx.containers.Panel;
import mx.controls.ProgressBar;

import mx.controls.SWFLoader;
import mx.core.UIComponent;
import mx.events.FlexEvent;

public class SlideViewer extends Panel {

private var loader:SWFLoader;
private var progress:ProgressBar;

private var swfFileName:String;

public function SlideViewer() {
super();

addEventListener(FlexEvent.CREATION_COMPLETE, init);
}

private function init(event:FlexEvent):void {
trace("SlideViewer initialized.");

loader = new SWFLoader();

// load the SWF directly, if provided with a filename
if (swfFileName){
loadSWF(swfFileName);
}
}

public function set swfName(value:String):void {
swfFileName = value;
}

public function loadSWF(swfName:String):void {
swfFileName = swfName; // in case we're coming from an external call rather than through the constructor

trace("Loading... " + swfFileName + ".swf");
loader.source = swfFileName + ".swf";
loader.addEventListener(Event.COMPLETE, swfLoadComplete);
loader.addEventListener(Event.INIT, swfLoadInit);

progress = new ProgressBar();
progress.source = loader;
addChild(progress);

loader.load();
}



private function swfLoadInit(event:Event):void {
addChildAt(loader, getChildIndex(progress));
}

private function swfLoadComplete(event:Event):void {
MovieClip(loader.content).stop();
// change the title of the Panel component
this.title = swfFileName;

// remove the progressbar
removeChild(progress);
progress = null;
}
}

}The init() function should only be called once, when the component is instantiated so I don't see why we're

And here is the output log capture:

(fdb) continue
SlideViewer initialized.
Loading... slides/productInfo.swf
[SWF] slides/productInfo.swf - 48,003 bytes after decompression
SlideView initialized.
[SWF] slides/productInfo.swf - 48,003 bytes after decompression
SlideView initialized.
Player session terminated
[Shutting down FDB]

Note that the SWF appears to be loading or instantiating twice (the "SlideView initialized" trace comes from the SlideView document class embedded in the SWF).

What the?

tomaugerdotcom
August 4th, 2009, 09:01 PM
Basically what's happening is it's loading once when I set the loader.source to the file I wanted it to load. Then I'm forcing it to load again when using loader.load();

This only occurs when you add the child to the display list.

I'm sure this is documented somewhere but the Adobe docs are so convoluted.

Anyway, hope this helps someone.

T

pardoner
August 4th, 2009, 10:59 PM
try removeEventListener(FlexEvent.CREATION_COMPLETE, init); inside the init function

tomaugerdotcom
August 4th, 2009, 11:02 PM
Yeah, tried that as one of the first things I did. That has nothing to do with it.

What's causing it is that in the code the load() triggers twice: once because we add the "source" parameter and add the child to the display list, the second time because I explicitly call its load() method.