Accessing the Actionscript Panel
An important note before we start. To access the actionscript panel in Flash you can either press F9, or click on the Actions - Frame panel on the bottom of the default Flash workspace.
Super easy variables in Flash
myVar = 10;
Which sets the variable called myVar to 10. But this isn't all that Flash can do with variables.
Declaring Variables the proper way
For instance you could do something like so:
var myVar = 10;
The myVar portion of the code is setting th evariable name and then = 10 is setting myVar to 10.
Variable Types and their quirks
One thing that Flash takes from lower level programming languages is the ability to specify the variable type, such as in C/C++ where you declare a whole number by using int myVar = 10;. However Flash takes a different approach and allows you to decide if you want to specify the variable type, or just have it a generic variable.
If you were wanting to create the same variable as above and only allow it to be a number (this includes floats and ints, basically any and all numbers) you would change the code to specify the variable type:
var myVar:Number = 10;
That creates the variable myVar as a number and sets it equal to 10. Now, ifyou were to take the same code as above and change it to a string it would return an error. var myVar:Number = "Text"; would return Type mismatch in assignment statement: found String where Number is required.
So, now I know that many of you are wondering why you would ever want to declare a variabel type. What does it do for you?
Luckily, Flash has also added in some nice parser functions if you declare the variable type. Once you have done the declaring and use the variable again the parser will give you auto-completion options to make the coding go faster, and let you know all the functions that are associated with that variable type.
For instance, if you are using a string variable var myStr:String = new String("Text"); and then wanted to trace (output) the value that myStr has you could do something like so trace(myStr.toLowerCase()); which would trace the value as all lower case characters. The other possible functions that are available for strings when you use the dot property of the string myStr. are as follows:
charAt charCodeAt concat fromCharCode indexOf lastIndexOf length slice split substr substring toLowerCase toUpperCase
Each of which give you tips on how to use them.
Before we end
Now you should have a fairly decent understanding of Variables in Flash, if you want to find all of the available variable Types in your version of Flash (works at least in Flash 8), in your Actions Frame click the blue plus sign (+), the tooltip is "Add a new item to the script", in the top left of the textbox and scroll down to Types.
I hope you were able to learn a lot from this tutorial and understand why it is useful to declare your variables and their types.