Main Content
Variables
Hello and welcome. Today we shall be leaning about one concept of Flash, variables. Well, to be more specific, AS2 and AS3 top-level data types. Variables are integral to Flash usage. These hold one and only one value, so you can access them later.
Creating (declaring) a variable
You can create a variable like this:
var a_variable;
Variable names cannot contain spaces, and I wouldn't use punctuation. Avoid words like "stop", these tend to be reserved for Flash, and so won't work. Whilst declaring a variable, you could also give it a value straight away:
var a_variable = "Hello";
The "var" is actually optional there in AS2, but you should put it in anyway; in AS3 it is compulsory. To make your code even clearer, you should put:
var a_variable:String = "Hello";
The ":String" bit tells Flash what type of variable that variable is, in this case a String. You don't have to put that in, but it will help later on, because Flash will quickly know if you are trying to do something a little stupid like trying to turn an number into uppercase, rather than you finding out after 50,000 people have had their versions of your Flash program break in front of them and are now disgruntled with you.
Changing the value of ((re)assigning) a variable
To change a variable you can say:
a_variable = "Goodbye!"
or to refer to it somewhere else:
var a_variable = "Good" var b_variable = a_variable + "bye!"
A bit of terminology now:
DECLARE : to create a variable.
ASSIGN : to give a variable a value.
= : the assignment operator. If you want to set a variable to something, you put that in.
Types of Variables (Top-Level Data)
There are lots of different types of variable, and I will define the major ones now. Always try and be as specific as possible; if you want to hold the number legs something has, don't use a type that accepts -1 as a value.
Both AS2 and AS3
- String
-
A string is a piece of text, which you have quote marks (" or ') around. Examples include "hello!" or "24" which are both strings as they are both enclosed in quote marks. Strings are commonly used in lots of circumstances that involve text.
var exampleA:String = "this is a string"; var exampleB:String = 'so is this'; var exampleC:String = "so is this, even with ~{:@~4[#;"; var exampleD:String = "";//An empty string var exampleE:String = "x";//A single character var exampleF:String; //A null string
- Number
- Pretty self-explanatory. Can hold decimals or integers; positives and negatives; (unlike other programming languages).
- Boolean
- Can only hold either be "true" or "false". 1 equates to true, 0 equates to false. Useful for things like hasGun or something less sinister like hasBunnyRabbit.
- Object
- Too complicated to go into here; It is only a simple data type in AS2, not AS3 so I shall be ignoring it.
Just AS3
- int
- Can only hold integers (whole numbers), between -2,147,483,648 and 2,147,483,647; I'm guessing that that would be sufficient for most purposes!
- uint
- Will only hold positive integers between 0 and 4,294,967,295 inclusive.
- Null
- The Null data type contains only one value, null. This is the default value for the String data type and all classes that define complex data types, including the Object class.
- void
- The void data type contains only one value, undefined. In previous versions of ActionScript, undefined was the default value for instances of the Object class. In ActionScript 3.0, the default value for Object instances is null.
There are other types of complex data and such like, but we will leave them until they are required, which isn't today. I hope this has been useful as a reference to you. If I had to say one thing, it would be this: type your variables.
Harry.