Lesson 2

The package

What exactly now is a package? A package allows more than classes, but only one class is public. We will soon learn what that means, for now keep that in mind. A package has certain rules regarding the accessibility of variables or functions.

So let's look at a typical package. The movie you see looks similar as the previous movie except that the text is larger. Let's look at the script.


package 
{
	import flash.display.Sprite;
	public class Starter_2 extends Sprite 
	{
	       public function Starter_2 () 
	       {
	               myTest();
	       }
	       private function myTest():void 
	       {
	               var a:Suplemental_1 = new Suplemental_1 ();
	               a.thisTest ();
	               addChild(a);
	       }
	}
}
import flash.display.Sprite;
import flash.text.TextField;
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);
	}
}
		

As you can see we need to import all classes also for the second class to function. However, if we add more classes we need to import all classes only once. The exception is the public class, for which all classes have to be imported separately from the subclasses. I don't need to go into details except for this code:


var a:Suplemental_1 = new Suplemental_1 ();
a.thisTest ();
addChild(a);
		

This syntax is slightly different from AS2. The first 2 lines are the same. We create a new instance of the class (instance variable is a in this case). Then we execute the subfunction (thisTest ()), but in order to display the contents of the function we need to add the method addChild(a). Without that the function is still executed, which might be useful, if there is a variable in the function defined, which we want to access, but nothing will be displayed.

previous  next