Categories
Featured jobs
» More ActionScript, Flash and Flex jobs.
» Advertise a job for free
Our network
Advertisement

 »  Home  »  Tutorials  »  Flash  »  Beginner  »  Variables 101

Variables 101

By Jesse Stratford | Published 09/9/2005 | Beginner | Rating:
Jesse Stratford
Jesse lives and works in Melbourne Australia. He is the Cofounder and a Director of http://ActionScript.org. A Flash enthusiast, teacher, author, freelancer and speaker Jesse enjoys participating in the http://ActionScript.org community and the wider Flash scene when he has time. 

View all articles by Jesse Stratford
Page 1 of 3
Written by: Jesse Stratford
Time: 30 - 45 minutes
Difficulty Level: Beginner
Requirements: Flash 5 or higher.
Topics Covered: What variables are and how we use them in Flash Actionscript.
Assumed knowledge: Very little.
The purpose of this tutorial is to provide a fundamental understanding of the purpose and application of variables in Flash ActionScript (both 1.0 and 2.0). This tutorial is aimed at beginners with no programming experience but is also suitable for programmers accustomed to other languages who wish to learn about Flash's syntax.

What is a Variable?

A variable is the simplest means of keeping track of information in your Flash code. A variable's value can be set once and never changed, or changed often. Values can be set during author-time (when you're creating your Flash file), or determined during run-time, like the score in a game.

Variables can be thought of as named boxes in which we store information. In order to access the information again we need to know the name of the variable which holds it. We provide the name by which the variable can be referenced when we create (or declare) our variable. Declaration let's Flash know that we'd like to create a new variable with a set name to store information in. Of course, a variable with no value is of little use, so generally we wish to define our variable also. Defining a variable is the act of allocating a value to the variable. In Flash, declaration and definition are generally combined into one statement of the form:

variable_name = some value;
For example, to record that my name is Jesse, we could declare and define a new variable called 'myname' as follows:

myname = "Jesse";
This code consists of the variable definition (which states we are creating a new variable called 'myname') and the value of this variable which is the string "Jesse", (more on strings later). The semi-colon at the end of the line signifies the end of our ActionScript command (or statement). Flash is quite forgiving if you leave off semi-colons at the end of statements, but strictly-speaking they are required.

A Note On Variable Naming:

In the above example, the variable name myname is purely arbitrary; we could have called it 'name' or pretty much anything else. Note however that variable names may not contain spaces or be the same as a reserved Flash keyword (see Macromedia's documentation for more information). Some people also consider it 'bad form' to begin a variable with a number or use hyphens in variable names (instead perhaps use an underscore).

Try the basic test below. Take careful note of things like the quotes around the name and the semi colon at the end of the line:


Assuming you got that one right, let's move on.


Spread The Word / Bookmark this content

Clesto Digg it! Reddit Furl del.icio.us Spurl Yahoo!

Related Articles
Comments
  • Comment #1 (Posted by gaurav - mms_gaurav at yahoo.com)
    Rating
    how can I use this veriable in flash u not clear this topic
    so sorry I couldn't use this ...
    I don't know where or in which layer I write myname = "gaurav'

     
  • Comment #2 (Posted by Jeff - jeffr at jeffandjessi.com)
    Rating
    your review of variables is great ,however how can I build a string and reference that? like ....

    loadVariablesNum("$myvar");

    I want to be able to build the string with a random number for a filename that will be opened?

    Jeff
     
  • Comment #3 (Posted by jared - jcsalzano at yahoo.com)
    Rating
    what do theese mean?

    myname = "adsf"

    so? whats that??
     
  • Comment #4 (Posted by Kevin Feeman - ksfeeman at yahoo.com)
    Rating
    Very good, though didn't take as long as stated, only about 15 minutes. However, being an extreme newbie at actionscript, it gave me a good foundation to move forward
     
  • Comment #5 (Posted by Sarah - sarahcwelsh at gmail.com)
    Rating
    Good Article. My Prof used it in our intro to flash class
     
  • Comment #6 (Posted by Gaurav - gaurav_webdesigner at yahoo.com)
    Rating
    thanx Jesse
     
  • Comment #7 (Posted by kiran - kumar)
    Rating
    wat is an out put statement in flash like javascript "documnt.write"
     
  • Comment #8 (Posted by Dean Blasutti - dawgydude at hotmail.com)
    Rating
    Great intro.
    It really helps me get used to the language of actionscripting, and i can see Java and this are very alike in naming conventions.

    However, some people don't seem to truly understand the meaning of this article, so I'll explain.

    In all respects, this lesson will teach you nothing that will provide any visual effects. Once you understand what variables are and how to create them, you can learn to use them. Variables are simply a means to store something, like a number or a string.

    Once you actually understand that, you will see in later lessons that you can use these variables for nearly everything you could possibly imagine. Right now you don't have any programming knowledge so you can't use variables for anything.

    Hope that cleared up guarav and jared's concerns.
     
  • Comment #9 (Posted by Frank - franciscom.camacho at yahoo.com)
    Rating
    I believe everybody misses a step when it comes to setting variable values... How to obtain it's value from the user of the movie himself. Say, if I want to ask the user`s name, for instance, I must give the user the means to do it, a text input box, a checkbox or another form object that gives me some information about whatever I'm asking the user for.

    Greetings from Colombia: Frank.
     
Submit Comment



Search Entire Site
Add to Google
Advertisements
Article Options
Latest New Articles
Set up a simple IIS Server for Flash
by Peter McBride

Day 1 at FITC Toronto 2008
by Anthony Pace

Simple reflection effect with AS2
by Jean André Mas

ActionScript.org Meets Josh Tynjala (aka dr_zeus)
by ActionScript.org Staff

Rapidly Create Online Flash Movies to Help Users Market, Sell and Support Software and Hardware
by Sabrina F

mailing list
Enter your email address:
mailing list
Subscribe Unsubscribe
© 2000-2007 actionscript.org! All Rights Reserved.
Read our Privacy Statement and Terms of Use...
Our dedicated server is hosted and managed by WebScorpion Webhosting.