Introducing ActionScript
What is ActionScript?
A scripting language extends the function of a computer program by allowing its user to control the main program's behavior in powerful and precise ways. Scripting differs from programming in that it depends upon a host program -- in our case, the application called Macromedia Flash, or the Flash Player, running in a Web browser.
Generally speaking, scripts are much less complex than programs. This means you can do very impressive things in a scripting language like ActionScript without having to master the enormous detail required for true programming. Thus you can stay focused on creativity and expressive purposes, yet still exercise significant command of your tools.
ActionScript, the native scripting environment of Flash since version 3, belongs to a family of languages called ECMAScript, named for the European Computer Manufacturers' Association, who standardized scripting languages at the turn of the century. Thus it has always been very similar (now in fact identical) to JavaScript, the scripting language initially designed by Netscape Communications for its Navigator browser. As the name suggests, JavaScript also bears some resemblance (though the relationship is tenuous) to the programming language Java, created by Sun Microsystems. Like Java, all ECMAScript variants base their syntax on the UNIX programming language called C. Because of these family resemblances, learning ActionScript or JavaScript will teach you much about the ways programmers work and think. Scripting languages may be the do-it-yourself version of industrial tools, but they'll still drive nails and cut wood, metaphorically speaking. While learning to script won't make you a programmer, you will be much better able to work with those masters -- capable of understanding the problems we all face when doing things with code.
Object orientationOriginally, ActionScript was used for very limited purposes, such as controlling playback in the timeline, on the model of the old scripting language Lingo in Macromedia Director. As Flash and ActionScript developed, however, this practice was replaced by more powerful strategies.
The current version of ActionScript, 3.0, provides robust support for object-oriented development. In object orientation, scripts are deployed in external ActionScript files, text files identified by the extension .as, and attached to objects defined within the main movie file (.fla) by a special procedure.
External files are linked to Flash symbols (or objects) as class definitions. In object-oriented programming languages, such as C++ and Java, a class is a discrete body of code that defines the character and operation of one or many objects.
Beginning with Lab 1, you'll learn how to write and use class definitions. All your scripting experience will involve object orientation. Thus your work with Flash will introduce you to the major organizing structure of all modern scripting and programming.
Functions
Quite often scripting commands are placed within a function. Functions commonly occur within object-oriented class definitions. In fact, all classes contain at least one function, caled a constructor.
A function is a wrapper or container that houses instructions that can be brought into play flexibly. For our purposes, all functions will have this form:
public function functionName(parameters):void
{
}
The term public indicates that this function is visible to, and usable by, other classes attached to the main movie. You may also define private functions, but we probably won't have any occasion to do so.
The term function indicates that you intend to define a function. The functionName can be anything you like, provided you don't use a word that is already used to name something else either in ActionScript or your particular movie. The parentheses that follow the function name often contain nothing, or they may contain one or more parameters, which are variables used by the function.
The last term, void, indicates that this function does what it does without returning a value. Later in the course, we'll create functions that return values, and so say something other than void. Most of your functions will follow this pattern, though.
Here's an example of an actual function:
public function moveRight():void
{
x ++;
}
When executed, this function causes the object to which it is attached -- most likely a MovieClip or Sprite -- to shift its horizontal position one pixel to the right. The variable x is defined in the Flash Player as a property of Movie Clips (or Sprites) that records and sets the horizontal position of the object on the stage. The mathematical operator ++, also defined in the Player, adds 1 to the current value of the variable. It is the same as writing:
x = x + 1;
Functions must be invoked. Sometimes functions are invoked by name, for instance:
moveRight();
The statement that invokes a function is usually found within another function. For instance:
public function animate():void
{
moveRight();
}
Manipulating Movie Clip properties
In the example above, we manipulate a property of whatever Movie Clip contains the moveRight() script. In this case the property is x, or horizontal position. As explained above, simply indicating the property allows us to change that property for the object linked to the present script. Sometimes, however, we may want to change the properties of other objects, as in this example:
redBall.x ++;
The line above is read "redBall-dot-x," and so the style which this example demonstrates is called dot notation. Dot notation expresses relationships among objects such that the item on the right of the dot belongs to the item on the left. Dot notation can also refer to fairly complicated chains of ownership, as here:
(root as MovieClip).object_A.leftSide.width
In this example the terms object_A and leftSide are assumed to be names given to objects. The object leftSide is a component or child of object_A. The term (root as MovieClip) has a particular meaning (as well as peculiar syntax), which we'll discuss later in the course. The term width indicates the horizontal dimension of the object leftSide.
Variables
Properties represent a special kind of variable. As in other scripting and programming languages, ActionScript variables are virtual containers that store values. These values can be assigned, interrogated, and modified.
Variables come in two types. Private variables contain values that can only be used by the script in which they occur. Public variables can be inspected and modified by any script within your movie. We'll encounter examples of each kind as we go.
Variables must be formally introduced, or declared, either at the start of a class file (if they are general, public variables), or at the start of a particular function, if they are private variables used only within that function. Variables are declared with the var statement, as in this example:
public var numHits:Number;
This statement defines a variable called numHits, which is accessible to any function in the current class. The variable is of type Number, meaning it must be assigned a numeric value.
You may also set a value for a variable when you introduce it. This practice is called initializing the variable, as in this example:
pubic var beenThere:Boolean = false;
This statement creates a public variable called beenThere of the type Boolean, which is a binary type that has two possible values, true and false. (You may also assign the numeric values 1 for true, or 0 for false.) The initial value of the variable in this case is set to false.
If you do not set an initial value for a variable, it will default to a null value, which means various things, depending on variable type.
Assignment versus evaluation; conditions
To place a value within a variable, use the assignment operator = (a single equal sign). So the statement:
n = 3;
means let n equal 3 or assign the value 3 to the variable n. We assume n has been initialized with an earlier var statement giving it a type of Number.
Note that the meaning of the single equal sign differs from the meaning it has in mathematics or logic, where n=3 means that n is equal to 3, not that n becomes equal to 3.
This distinction may not seem especially important until we introduce conditions. A conditional statement, in scripting as well as in ordinary language, is an if/then construction. If the condition is met, then the consequence occurs. If conditions are basic building blocks of all scripts and programs.
However, when we need to determine if one thing equals another within a conditional statement we can't use the single equal sign, since it is used to assign a value. We use a double equal sign instead:
if(n == 3) moveRight();
The statement above instructs Flash to execute the moveRight function if the variable n contains the value 3. The statement is read, "if n is 3, then execute function moveRight." Note the term "then" is represented by the space that follows if(n == 3). All languages derived from C use this convention.
When more than one consequence follows from a single condition, we use a slightly more complicated form of the conditional statement:
if(n == 3)
{
beenThere = true;
moveRight();
}
The curly braces {} indicate a compound consequence. Both statements within the braces depend on the initial condition. Note the use of the single equal sign as an assignment operator in the first line within the braces.
Suppose we want something to happen only in case two conditions are both met. In that case, we'd use a compound conditional clause like this:
if(n == 3 && q > 47)
{
beenThere = true;
moveRight();
}
The double ampersand && is a logical AND operator that chains two conditions together. You may also use a logical OR, which is represented by two vertical lines or "pipes" (||). In an OR situation the consequence occurs if either condition is met.
Finally suppose you want to specify one consequence if a condition is true and a second consequence if it is false. The If/Else condition serves this purpose:
if(n == 3)
{
moveLeft();
}
else
{
moveRight();
}
The code in this example executes the moveLeft function only if n has a value of 3. If n has any other value, it invokes moveRight instead.
Loops
Along with conditions, scripts and programs often depend heavily on loops. A loop tells a program to execute certain instructions a specified number of times, or under specified conditions. The most common form is the For loop. Here is an actual example of such a structure:
for(var i:Number=1; i<6; i++)
{
outputField.text += i + "<br />";
}
This loop takes the value of its index variable i five times and places that value on a separate line in a text field called outputField (an arbitrary name). The HTML "<br />" is appended in order to place the values on separate lines. The result looks like this:
1
2
3
4
5
The syntax of the For loop in ActionScript is exactly the same as in C, PERL, Java, and JavaScript. In the initial parenthesis, three statements occur. The first sets a starting value for the index variable. In ActionScript 3, the index variable must be declared with an embedded var statement. This value may be any number you like. The second statement sets a limit for the index variable. Limiting to numbers less than 6 means the loop will stop when i reaches 5. The third statement indicates how the loop is incremented. The instruction "i++" means that the variable i increases in value by 1 with each repetition of the loop. The increment may also be varied. Writing "i+=2" would cause the index to increase by 2 each time.
That's all for now
While we've covered a fair amount of material here, this introduction only begins to scratch the surface of ActionScript. We'll encounter many other instructions and control structures as we go further. These are enough to get you started, however.
|
|