This Class works, but I have some questions

Hi all,

I have this loader class which I’m able to instantiate in my .fla and pass parameters to just fine, but I don’t understand why I had to do what I did to make it it work.

For example, I had to further declare my private variable using “this.myVar = myVar”, once I did that, the other functions were able to access those vars without getting a “null” error. Here is the class:

package nexus.preloaders
{
	
	import flash.display.*;
	import flash.text.*;
	import flash.net.URLRequest;
	import flash.events.*;
	import com.greensock.TweenLite;
	import com.greensock.easing.*;

	public class ExternalLoader extends Sprite 
	{

		private var loaderStatus:TextField;
		private var swfName:String;
		private var customLoadingText:String;
		private var loaderTextYPosition:Number;
		private var loaderTextXPosition:Number;
		private var loaderTextSize:Number;
		private var loaderTextColor:uint;
		private var loader:Loader = new Loader();

		public function ExternalLoader(swfName:String, 
									   customLoadingText:String, 
									   loaderTextXPosition:Number, 
									   loaderTextYPosition:Number,
									   loaderTextSize:Number,
									   loaderTextColor:uint)
		{
			//the following ensures that the functions outside of this function 
			//have the private vars passed to them
			this.customLoadingText = customLoadingText;
			this.loaderTextYPosition = loaderTextYPosition;
			this.loaderTextXPosition = loaderTextXPosition;
			this.loaderTextSize = loaderTextSize;
			this.loaderTextColor = loaderTextColor;

			//Add the event handlers
			loader.contentLoaderInfo.addEventListener(Event.OPEN, onOpen);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

			loader.load( new URLRequest(swfName))
		}

		private function onOpen (event:Event):void
		{
			loaderStatus = new TextField();
			TweenLite.from(loaderStatus, 1, {alpha:0});
			var loaderStatusFont:Font = new LoaderStatusFont();
			var vtf:TextFormat = new TextFormat();
			vtf.font = loaderStatusFont.fontName;
			vtf.size = loaderTextSize;
			vtf.color = loaderTextColor;
		
			loaderStatus.antiAliasType = AntiAliasType.ADVANCED;
			//loaderStatus.wordWrap = true;
			loaderStatus.embedFonts = true;
			loaderStatus.defaultTextFormat = vtf;
			//loaderStatus.setTextFormat(vtf);
			addChild(loaderStatus);

			//loaderStatus.x = (stage.stageWidth / 2) - loaderStatus.width / 2;
			loaderStatus.x = loaderTextXPosition; 
			loaderStatus.y = loaderTextYPosition;
		}

		private function onProgress (event:ProgressEvent):void
		{
			var perc:Number = event.bytesLoaded / event.bytesTotal;
			loaderStatus.text = customLoadingText + Math.ceil(perc*100).toString()+"%";
			//var percent:Number = event.bytesLoaded/event.bytesTotal * 100;
			//loaderStatus.text = customLoadingText + percent + "%";
			loaderStatus.autoSize = TextFieldAutoSize.LEFT;
			
			if (Math.ceil(perc*100) >= 90)
			{
				TweenLite.to(loaderStatus, 1, {alpha:0});
			}
		}

		private function onComplete(event:Event):void 
		{
			removeChild(loaderStatus);
			loaderStatus = null;
			addChild(loader);
		}
		
	}//end public class function
	
}//end package

Also, I’m not use to using an “OPEN” listener for my loader object, I just normally use PROGRESS and COMPLETE. But in this case, the onOpen functions establishes the dynamic text field, if i try moving the text field out of that function, it won’t work.

Any observations here would be a appreciated.

btw, feel free to use the code however you want. This is a very convienent loader class, at least it is for me its. It’s my first class so I’m kinda stoked about that. (although the core loader code came from a tutorial somewhere…) You can set the name of the swf you want to load, the x and y position of the loading text, the wording of text and the font size, color all within the constructor in your host .fla file, see below. I know, to some of you coding gods, this is not a big deal, but thought i’d share anyways…

here is how to instantiate it in your fla:

var myLoader:ExternalLoader = new ExternalLoader("cool.swf", "Building Main ", 255, 280, 20, 0x0000CC);
//The ExternalLoader constructor receives a list of parameters:
//swfName: The file name of the loading swf.
//customLoadingText: Text displayed while load is in progress.
//loaderTextXPosition: X position of loaderStatus.
//loaderTextYPosition: Y position of loaderStatus.
//loaderTextSize: Size of the loaderStatus font.
//loaderTextColor: Color of loaderStatus font in hex.
addChild(myLoader);

NOTE: DESIRED FONT MUST BE PRESENT IN LIBRARY OF THE .FLA
IN WHICH THE CLASS IS INSTANTIATED AND SET TO EXPORT
AS "LoaderStatusFont"