Variables 101

Page 3 of 3
Jesse Stratford
Jesse lives in Melbourne Australia and is the Cofounder and webmaster of http://ActionScript.org. Once a full-time Flash enthusiast, teacher, author, freelancer and speaker Jesse's now leads a double life -- management consultant by day, http://ActionScript.org director by night. He enjoys participating actively in 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
12 Responses to "Variables 101" 
|
said this on 05 Jun 2007 11:53:23 AM CST
how can I use this veriab
so sorry I cou I |
|
said this on 02 Aug 2007 5:16:59 PM CST
your review of variables
loadVariablesNum( I want to be able to Jeff |
|
said this on 25 Aug 2007 11:43:18 PM CST
what do theese mean?
myname = "adsf&q so? whats tha |
|
said this on 03 Oct 2007 12:43:01 PM CST
Very good, though didn
|
|
said this on 11 Oct 2007 9:12:13 AM CST
Good Article. My Prof use
|
|
said this on 05 Dec 2007 1:18:31 AM CST
wat is an out put stateme
|
|
said this on 18 Mar 2008 9:04:00 AM CST
Great intro.
It really h How In all respects, th Once you Hope t |
|
said this on 03 Apr 2008 4:30:45 PM CST
I believe everybody misse
Greeting |
|
said this on 01 Jun 2008 5:50:05 PM CST
Thanks I needed to know h
|
|
said this on 04 May 2009 2:35:53 AM CST
Thanks for this very info
|
|
said this on 09 May 2009 12:14:49 PM CST
thanks for showing me how
|



Author/Admin)