Lesson 10

Namespace

The namespace is a new feature in AS3. Basically namespace variables hold properties or functions and hide them until they are called. Move the mouse over the button and out and you see a different string displayed.

The script is located in Starter_14.as but I am only showing the important part of the script here. The rest of the script is a sample script from Adobe to create a button. I do not need to explain the first part any more.


package 
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	//
	public class Starter_14 extends Sprite 
	{
		

In the next part we declare the namespace variables Test_1 and Test_2 and all other variables.


	   public namespace Test_1;
	   public namespace Test_2;
	   private var button:CustomSimpleButton;
	   public var aField:TextField;
	   public var bField:TextField;
	   private var myIndex_a:Number;
	   private var myIndex_b:Number;
	   private var myIndex_c:Number;
		

Now there is something new. We fill the namespace variables with a value right here and not within the first function. The syntax is namespace var value.


	       Test_1 function test_1 ():void
	       {
	                       aField = new TextField ();
	                       aField.text = "This is test 1.";
	                       addChild(aField);
	                       if(bField!=null)
	                       {
	                               removeChild(bField);
	                       }
	       }
	       Test_2 function test_2 ():void
	       {
	                       bField = new TextField ();
	                       bField.text = "This is test 2.";
	                       addChild(bField);
	                       if(aField!=null)
	                       {
	                               removeChild(aField);
	                       }
	       }
		

Now we have our functions. I will explain the button eventhandlers in the final tutorial but not here, not to loose the focus.


	   public function Starter_14 () 
	   {
	       myTest();
	   }
	   private function myTest():void 
	   {
	       button = new CustomSimpleButton();
            button.x = 100;
	       button.y = 100;
            button.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
            button.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
	       addChild(button);
	       }
		

Next we call the namespace variables and there are two ways to do that as shown here for Test_1...


	   private function mouseOutHandler(event:MouseEvent):void 
	   {
	       use namespace Test_1; // calling namespace
	       test_1 (); 
	   }
	   
		

...or here for Test_2. Whichever way you prefer, although the second possibility is simpler and simple is better.


	       private function mouseOverHandler(event:MouseEvent):void 
	       {
	        Test_2::test_2 ();
	       }
	}
}
		

Now we come to a real tutorial example, a XML-based slideshow, where we combine a number of features we learnt so far.

previous  next