Archive for June, 2009

Local variables, unlike globals, are only available in the current script. In the next frame, the variable won’t exist. You can certainly create a new variable with the same name, but the previous contents from the last frame will not be in it.

The point of local variables is to create modular code. If a variable is local, it is removed from memory when the script is finished. Otherwise, if it is a global variable, the variable and its value will hang around until the movie ends.

To create a local variable, you need to use the var keyword. For instance, you could create a local variable named myLocal and place the number 9 in it like this:

var myLocal = 9;

After you set the variable with the var keyword, you don’t have to use var again in that local piece of code. For instance, the following code creates the local variable, sets it to 9, changes its value to 11, and then sends it to the Output window:

var myLocal = 9;
myLocal = 11;
trace(myLocal);

When deciding when to use local variables and when to use global variables, the rule of thumb is to always use local variables unless there is a good reason to use a global. We’ll mostly use local variables.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

A global variable is one that is accessible throughout the entire level of the Flash movie. You can set it in one frame, and it will still contain its contents in another frame.

You don’t need to do anything special to create a global variable. Just using it, like in the previous example, automatically makes the variable a global one.

In most programming languages, global variables are available everywhere. However, Flash movies use a system of levels. The main movie timeline is the root level. Any movie clips are actually small Flash movies inside the main one. The graphics and scripts inside a movie clip are one level down from the root level. Global variables at the root level aren’t accessible inside a movie clip—at least not directly.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

In Hour 3, “Learning to Program,” I mentioned variables. They are little containers for storing information.

Setting Variables
Using variables in ActionScript is easy. All you need to do is assign a value to a variable name. Here is an example:

myVariable = 7;

The preceding line creates the variable named myVariable and places the number 7 inside it. Note that the name myVariable was chosen arbitrarily by me. You could name the variable anything. For instance, numberContainer, a, or fred would all work.

To see variables in action, you can test them with the Output window. Here is a short program that you can place in the first frame of a blank movie:

myVariable = 7;
trace(myVariable);

When you run this movie, the Output window appears with the number 7 in it. The number 7 was stored in myVariable and then the trace command was used to place the contents of myVariable in the Output window.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

The movie takes a second to run. You end up with a blank window that represents your running movie. Because no graphics are in your movie, there is nothing to show.

Like the Actions panel, the Output window has a small pop-up menu in the upper-right corner. You can use it to copy the text in the Output window or clear all the text. You can also search the text in the window, save it to a file, or print it.

One last option in the pop-up menu lets you set the current debug level. You can choose only to have error messages displayed, have minor warnings displayed as well, or have no error messages displayed at all.

We’ll use the Output window throughout the rest of this hour and in many other times throughout the book.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

The best way to become familiar with the Output window is to use it. Let’s write a simple program that sends a message to the window.

Start Flash MX and create a new movie.

Select the default first frame of the movie and then open the Actions panel, if it is not already open. Expand the Actions panel so that it is much bigger than the default size.

Use the pop-up menu in the upper-right corner of the Actions panel to set the Actions panel mode to Expert.

Click in the script area of the Actions window so that you see an insertion cursor there.

Type the following script in the Actions panel:

trace(”Hello World”);

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

The Output window is a special programming tool that only appears if you are testing a movie while running Flash. Error messages and some other information appear in the Output window. In addition, the ActionScript command trace writes out information that you specify to the Output window.

The Output window is useful when you are debugging a program. You can use the Output window to track the values of variables and tell you what part of an ActionScript program is currently running.

The Output window can also be used to help you learn ActionScript. We can write short little programs that do nothing but send information to the Output window. This will help you see the results of your first simple program.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

The trace command sends its contents to the Output window. We’ll see more of that command in the upcoming section “The Output Window.”

The if command is called a conditional statement. It tests the statement that follows it, myVariable + 3 == 5, to see whether it is true. If so, the code segment in the brackets is executed. If not, this whole code segment is skipped.

The code segment in question is a single trace command that sends the variable myOtherVariable to the Output window.

The rest of the example consists of three close brackets. The first closes the if statement. The second closes the for loop, and the third bracket closes the on (press) segment.

Now that you have seen a complete, though small, ActionScript program, let’s take a closer look at some specific parts of ActionScript.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

The first line of this code segment creates a new local variable called myVariable. It assigns the value of 7 to it. The new line assigns the string “Macromedia” to the variable myOtherVariable. We’ll look more at Flash variables in the section “Local and Global Variables” later this hour.

The semicolon at this end of this line, and many other lines, signifies the end of the instruction. Put a semicolon after every line that is a complete instruction.

The for structure starts a loop. In this case, it loops 10 times, with the variable i starting at 0 and increasing to 9. We’ll look at loops in the section “Loops,” later this hour. The bracket at the end of the for line signifies the start of the code segment that will be the body of the loop.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

The first line of the script identifies the rest as something that executes when the user first presses the button. The on (press) construct can be used only in button scripts. You can also use on (release) if you want the code to execute when the user completes the button press.

The curly bracket, {, at the end of the first line signifies that this is the beginning of a code segment. From the open bracket, {, to its corresponding closing bracket, }, all the code inside the brackets is part of a set of code that belongs together. In this case, the code segment represents all the code that is executed when the button is pressed.

Notice that the indentations of the code example follow the brackets. The line after an open bracket is indented one tab stop farther, and every line that follows it is at the same level until the corresponding close bracket, or another open bracket takes the code one tab stop farther. This type of indentation makes it much easier to read the code and is common among computer languages. In fact, Flash will indent your code for you by default.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

When you write your own scripts, you will use all sorts of different keywords and characters. To learn these, it will be useful to look at a real script and examine its parts.

We’ll use the following script as an example. It is a button script that runs when the user clicks the button that the script is attached to. It doesn’t perform any particular function, but rather demonstrates several major ActionScript constructs.

on (press) {
var myVariable = 7;
var myOtherVariable = “Macromedia”;
for (var i=0; i<10; i++) {
trace(i);
if (myVariable + 3 == 5) {
trace(myOtherVariable);
}
}
}

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

  • Partner links