Understanding Macromedia Flash 8 ActionScript 2: Basic Techniques for Creatives

Chapter 9: Organizing Code with Functions

Functions

Functions are used to assemble statements into meaningful, logical functional units hence the name, Function. Even simple applications can have a large number of individual statements. Without functions, code would be unruly and unreadable.

ActionScript 2 code exists primarily in the form of classes. The methods of classes are functions. As explained in Chapters 5 7, classes take the idea of modular, reusable code to the next level and provide a way to group functions and variables together into logical units.

Defining Functions

Whether they are part of a class or simply defined on a timeline, functions are constructed in the same way. A function definition has three main elements:

  • The function Declaration

  • Parameters specified in the declaration

  • A Body containing statements

  • An optional Return Value the type of which is specified in the declaration

Take, for example, the mySquare( ) function definition from Chapter 5:

<b class="bold">function</b> mySquare (x:Number, y:Number, size:Number):  MovieClip {  <b class="bold">var</b> depth:Number = _root.getNextHighestDepth();  <b class="bold">var</b> square:MovieClip =    _root.createEmptyMovieClip ("example", depth);  square.beginFill(0x0022CC, 80);  square.lineStyle(2, 0xFF9900, 100);  square.moveTo(0, 0);  square.lineTo(size, 0);  square.lineTo(size, size);  square.lineTo(0, size);  square.lineTo(0, 0);  square.endFill();  square._x = x;<a name="339"></a><a name="N14528"></a>  square._y = y;  <b class="bold">return</b> square;}<b class="bold">var</b> tempSquare:MovieClip = mySquare(100, 100, 50);

The Declaration says that the mySquare( ) function expects 3 parameters, which are all Numbers, and that it will return a MovieClip object:

<b class="bold">function</b> mySquare (x:Number,...

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Vacuum Flanges and Fittings
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.