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?
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?