ActionScript Fundamentals pt. 1 / Variables
Subtitles of the Movie
We now get to the important concept of Variables. In almost all scripts you'll need containers for storing and retrieving information before, during, or even after the script has run. This information could include stuff such as mouse and keyboard events from users, or information from databases, external documents, websites and so on. These containers are called Variables and are fundamental to all scripting and programming languages. In English we could say that Ian has red hair. In ActionScript this statement would be expressed as follows. Variable ian haircolor colon String equals red and the red is in quotation marks, finished off with a semicolon there at the end and this is actually referred to as initializing the variable, or setting a value to the variable, but variables can and often do change, that's where they get their name. So let's explore this a bit. Let's go ahead and erase this line of code. Open up a new Flash file and name it variables.fla. Name the only layer on this Timeline action. Let's go ahead and do that. Change that from layer 1 to action with a lower case a. Go ahead and open your Actions Panel, choose Window, Menu, Actions, or press the F9 key. Click on the first and only frame in your Timeline, right there and then type the following code into your Actions Panel. First you want to add a comment: //create a variable and initialize. Variable myName colon String equals student. Let's go ahead and add another line after the green student, then add another comment: create a function to display the value of myName; myName is the variable. It's a string variable. Let's go ahead and add another line after that. I like to space out my code so they're easier to read. Then we're going to create a function, a function is in blue. That's a reserved term. We're going to name the function display underscore name open-close parentheses, then I'm going to have an open curly brace, myName equals in quotation marks james semicolon and then we're going to trace myName, that's going to actually print this out in the Output Panel so we can see it and then we're going to go ahead and run the function, display name. Once you've written out these lines of code and double checked that they're accurate and exactly as I've written it here, let's test the movie by choosing Control, Test Movie and then here in the Output Panel we're going to get the result James. I had to drive home the point that these variables can change. Let's take the James out and type John like so. Test the movie again and this time John will be traced in the Output Panel. And one more time here, Mary, so go ahead and try this. Change the myName variable here in that line of code and then check your new output by Control, Testing the movie again and notice that the variable changes each time. So to summarize our script work we're creating and initializing a variable by setting an initial value of that variable, a string variable, to student. We're then creating a function to display the value of this variable, right here, function display name. Then we're changing the variable by declaring myName equals and then changing the content in between those two quotation marks, then we're tracing this variable by printing it out in the Output Panel and then we're running the function to then do everything in between these two curly braces. Now in ActionScript basic statements are referred to as Expressions, so let me now mov on to the next movie and review in more detail this concept of Expressions in ActionScript 3.0.
Tutorial Information
| Course: | Adobe Flash ActionScript 3.0 for Designers |
| Author: | James Gonzalez |
| SKU: | 34060 |
| ISBN: | 1-935320-82-3 |
| Release Date: | 2009-11-09 |
| Duration: | 9.5 hrs / 101 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 