Lesson 3

Calling external classes

Instead of one package we can also interact with several packages. Subclasses in a package are often helper files like a class to create movieclips for buttons or may be a class to load a movie, while the main function is located within the public class. Packages also allow the development of individual units by different developers, which later can be combined to create an application. The movie below is the same as the previous movie in lesson 2 but now we have 2 packages.

Package 1 is shown below. It is a simple package with a function call. However, compared to when the second class was located within the same package we now need to import the class import supplement.Suplemental_1; writing out the whole path.


package 
{
	import flash.display.Sprite;
	import supplement.Suplemental_1;
	public class Starter_3 extends Sprite 
	{
	       private var a:Suplemental_1;
	       public function Starter_3 () 
	       {
	               myTest();
	       }
	       private function myTest():void 
	       {
	               a = new Suplemental_1();
	               a.thisTest ();
	               addChild(a);
	       }
	}
}
		

The second package is in a folder named supplement and looks like this:


package supplement {
	import flash.display.Sprite;
	import flash.text.TextField;
	public class Suplemental_1 extends Sprite 
	{
	       private var tField:TextField;
	       public function Suplemental_1 ()
	       }
	       public function thisTest ():void
	       {
	               tField = new TextField();
	               tField.autoSize = "left";
	               tField.background = true;
	               tField.border = true;
	               tField.x = 200;
	               tField.y = 200;
	               tField.text = "Hello You, what is your name?";
	               addChild(tField);
	       }
	}
}
		

It is similar to what we have seen before already. However, there is one difference, the word package is followed by the name of the folder, where the script is located. If there are more folders above we need to write them as well separated by a dot (.).

previous  next