As2 to As3 Baby Steps - Part 4 - Loading files
Reynaldo November 9th, 2007
The MovieClipLoader class in AS2 has now been replaced by the new and improved Loader class in AS3. Something key to keep in mind is that the Loader Class itself does not dispatch any events (something I learned the hard way,
). Instead the events are fired from Loader.contentLoaderInfo.
This is how you would load a class using MovieClipLoader in AS2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | var mcLoader:MovieClipLoader = new MovieClipLoader(); var objListener:Object = new Object(); var mcLoaderClip:MovieClip = this.createEmptyMovieClip("mcLoaderClip", this.getNextHighestDepth()); objListener.onLoadInit = function($target:MovieClip):Void { trace($target + " loaded."); }; objListener.onLoadProgress = function($target:MovieClip, $bytesLoaded:Number, $bytesTotal:Number):Void { trace(Math.round(($bytesLoaded / $bytesTotal) * 100) + " percent loaded so far."); }; mcLoader.addListener(objListener); mcLoader.loadClip("someImgFile.jpg", mcLoaderClip); |
Now here this is how we do it in AS3 using the new Loader class and the URLRequest Class mentioned in Baby Steps Part 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | var objLoader:Loader = new Loader(); var objFileToLoad:URLRequest = new URLRequest("someImgFile.jpg"); objLoader.load(objFileToLoad); objLoader.contentLoaderInfo.addEventListener(Event.INIT, imageLoaded); objLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, showProgress); function imageLoaded($event:Event):void { this.addChild($event.target.content); trace($event.target + " loaded."); } function showProgress($event:ProgressEvent):void { trace(Math.round(($event.bytesLoaded / $event.bytesTotal) * 100) + " percent loaded so far..."); } |
There’s a reason why I chose to listen to the INIT event and not the COMPLETE event once the file loaded. Basically, according to Adobe’s Live Docs referring to the Loader Class the COMPLETE event gets fired after the file has been loaded, but more specifically, the INIT event gets fires when the properties and/or methods of the loaded file are accessible.
Happy coding!




In the AS2 ex you loaded the clip into mcLoaderClip
Where, in the AS3 example, is the clip referenced which will hold the jpg?
Thanks.