Variables 101

Page 3 of 3
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 StratfordChanging Variable Types
Being a scripting language, ActionScript is generally very obliging if you wish to convert a variable's value from one form to another or allocate a variable a value of another type. For instance, the following code, which creates a variable, assigns it a string value, then overwrites that value with a numeric value, is perfectly acceptable:
fav_color = "purple";
fav_color = 1;
This flexibility is often very useful but it can also make tracking down bugs caused by assigning variables the wrong type of value (which you later try to manipulate using the wrong operations) very difficult. For instance, if you accidentally assign a variable a string value and then try to add it to a numeric value, Flash makes both values Strings and concatenates them together. In a small script this isn't such an issue, but in a full Rich Internet Application it can be a bug finding nightmare. Strict Data Typing, addressed below, helps to avoid this issue.
Strict Data Typing
ActionScript 2.0 in Flash MX (Pro.) 2004 introduced the concept of Strict Data Typing which allows developers to specify the single acceptable type for a variable when the variable is declared/defined. Should you inadvertently try to assign a value of the wrong type to a strictly typed variable, you will receive a "Type Mismatch" error when publishing your movie. To strictly type a variable you declare/define it as normal, then add a colon and the datatype after the variable name. You must also use the var operation at the beginning of your declaration/definition for scoping reasons.
var fav_color:String = "purple";
This declares and defines a new variable called fav_color which may only ever contain String values.
Strict Typing can also be applied to function return types and arguments:
function doSomething(word:String):Number {
}
This defines a function which must take a String argument and must return a Number.
Finally, note that when using Strict Data Typing you may bind a variable or function to custom datatypes; you are not limited to Flash's built-in types.
Fiddling
Now you know the basics of Flash variables. You know how to create them and the three major types. Paths are also important to learn about in relation to variables, so checkout my Paths tutorial to broaden your horizons. In the mean time, fiddle around with variables in any way you can think of!
| Jesse Stratford is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash. NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there. |
If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity. |
| This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it. |
Spread The Word
Related Articles
10 Responses to "Variables 101" 
|
said this on 05 Jun 2007 11:53:23 AM CDT
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' |
|
said this on 02 Aug 2007 5:16:59 PM CDT
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 |
|
said this on 25 Aug 2007 11:43:18 PM CDT
what do theese mean?
myname = "adsf" so? whats that?? |
|
said this on 03 Oct 2007 12:43:01 PM CDT
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
|
|
said this on 11 Oct 2007 9:12:13 AM CDT
Good Article. My Prof used it in our intro to flash class
|
|
said this on 05 Dec 2007 1:18:31 AM CDT
wat is an out put statement in flash like javascript "documnt.write"
|
|
said this on 18 Mar 2008 9:04:00 AM CDT
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. |
|
said this on 03 Apr 2008 4:30:45 PM CDT
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. |
|
said this on 01 Jun 2008 5:50:05 PM CDT
Thanks I needed to know how to set a variable to use in an if statement later in the animation to direct the user to one of two frames: I use a boolean variable on buttons to see if they pressed the true button or the false button. Then if they pressed true the animation starts over when it reaches the end and if it was false the animation will do end credits.
|



Author/Admin)