Is AS3 Ready For Prime-Time? (part 3) – taterboy
September 27th, 2008 | Filed under: Flash AS2 to AS3, Tips
Targeting Loaded swfs and Streaming
As we move into the Object Oriented mindset, communication between a host and loaded swf can be a little controversial. Though using bubbling events is great for easy plug and play type communication, establishing a direct connection to pass information can be more efficient.
As a comparison, to load an swf into an AS2 project, all you have to do is tell a MovieClip to load an swf and then that MovieClip will take on all the attributes of the loading swf. To access the loaded timeline you use, MovieClip.gotoAndPlay(2);
In AS3 all loaded swfs are loaded into a loader object that is added to a container. To access the timeline of a loaded swf you would use an instance of the Loader Object’s content.
var loadedSwf:Object = Loader.loaderInfo.content; //or var loadedSwf:Object = Loader.contentLoaderInfo.content; //then: loadedSwf.gotoAndPlay(2); |
Here are a few things to keep in mind when communicating with loaded swfs with AS3.
1. Use an object as a content instance if you need to access your loaded swf.
var newSwfContent:Object;
var ldr:Loader = new Loader();
var urlReq:URLRequest("content.swf");
ldr.load(urlReq);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, finHandler);
function finHandler(ev:Event):void{
newSwfContent = ev.target.content;
newSwfContent.gotoAndPlay(2);
} |
2. For the sake of organization it may be useful to reference multiple instances of loaded content in a single object. Like loading a bunch of images, it may be easiest to do something like this.
var imageHolder:Oject = new Object();
....
function finHandler(ev:Event):void{
imageHolder[imageHolder.length] = ev.target.content;
} |
There are many ways to set this up to have quick access to your content using the powerful Object Class.
3. Another way to target a loaded swf is to have the loaded swf request information from the host. One way to accomplish this is using dispatch event.
//Loading SWF:
this.loaderInfo.addEventListener(Event.COMPLETE,completeHandler);
function completeHandler(ev:Event):void{
dispatchEvent(new Event("LOADING_COMPLETE",true));
}
//Host File:
addEventListener("LOADING_COMPLETE",eventHandler);
function eventHandler(ev:Event):void{
trace("loaded: " + ev.target.currentFrame);
} |
4. In AS2 you could access the timeline of a loading swf almost instantly. It was more of a streaming environment allowing the access of code or objects on the first few frames while the rest of the swf continued to load. You can access the timeline before a complete load with AS3, but in all my testing I have not been able to get access (without error) before about 80% of the swf is loaded. Using That just does not help out as much as access at 10% with AS2.
Event.INIT is triggered before the swf is completely loaded still allowing access to variables and objects, but you can not assign the target’s content to an object before the load is complete. Any attempt to access the loaded swfs timeline before Event.INIT results in the following error:
“The loading object is not sufficiently loaded to provide this information.”
The following demo shows a host file with complete access to the timeline of the loaded SWF.
|
RSS feed for comments on this post. TrackBack URL
Good information, thx for that..
i definitly need to start with AS3…
Comment by ben — October 6, 2008 @ 6:01 am