Return to Kroll Design home page

Flash Tutorials (with .FLA files included)

 
« Return

ActionScript 3

Use 'include' to access external ActionScript

You can load external ActionScript at the time of publishing with the 'include' directive. In other words, the external code is included when your compile/publish the movie, so that by time someone actually goes to run the movie, the external code is no longer required to be in an external folder. Of course, you'll want to keep the external code should you desire to re-publish the movie later. Even if you don't modify the included code later, you still need for it to be present in the indicated folder whenever the .FLA file is published into an .SWF file. This example uses file 'as3_absolute_value.as' which is in the 'as' subfolder. Flash has a built-in method to compute absolute value, so I'm sure I could have chosen a better example, but the purpose of this tutorial is not to invent a brand new math function, but rather to show the include directive in action.

The example also processes user input data, removing blanks, then testing to see if what's remaining is a number. If the user input is a number, it calls the absolute value function contained in the external ActionScript.

Download the AS file

Download the FLA file

The syntax of the include directive in ActionScript 3 is a little different than in ActionScript 2. In ActionScript 3 you no longer have a pound sign (#) in front on 'include'. What's common to both AS2 and AS3 is that there is no semicolon at the end of the include directive.

Here's the contents of the external ActionScript file, as/as3_absolute_value.as:

function absoluteValue(yourNumber):Number
{
	if (yourNumber < 0)
	{
		return -yourNumber;
	}
	else
	{ 
	  return yourNumber;
	}
}

And here's the ActionScript from within the FLA file:

include "as/as3_absolute_value.as"

submit_btn.addEventListener(MouseEvent.CLICK, processInput);

function processInput(e:MouseEvent)
{
	var noBlanks:String = input_txt.text.split(' ').join('');
	
	if (noBlanks == "")
	{
		output_txt.text = "Please enter a number and click the 'GO' button";
	}		
	else if (isNaN(Number(noBlanks)))
	{
		output_txt.text = "Please enter a number and click the 'GO' button";
	}
	else
	{
	var abs_value:Number = absoluteValue(noBlanks);
	output_txt.text = "The absolute value of " + noBlanks + " is " + abs_value + ".";	
	}	
}


 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 12/31/1969 7:00 PM