The Variable.

Let's first look at the www.dictionary.com's definition:

var·i·a·ble

adj.

1. a. Likely to change or vary; subject to variation; changeable. b. Inconstant; fickle.

2.Biology. Tending to deviate, as from a normal or recognized type; aberrant.

3. Mathematics. Having no fixed quantitative value.


n.

1. Something that varies or is prone to variation.

2. Astronomy. A variable star.

3. Mathematics:

a. A quantity capable of assuming any of a set of values.

b. A symbol representing such a quantity. For example, in the expression a2 + b2 = c2, a, b, and c are variables

Source: www.dictioanary.com

 

For those of you who like analogies - think of a variable as a single sheet of blank paper. This piece of paper can have anything written on it - a word, a number, a formula, or a description about something else - pretty much anything you want. Putting a title on the page will help us quickly identify the contents of the piece of paper without having to read it's entire contents. Just think of a single word... a word like.... bob. Okay, bob is now the title for our new piece of paper. Now let's write something on the page... how about "blue" .. there you have it, that's the best i can do to assimilate a variable to the real word... A piece of paper with a title, and some stuff written on it.

Now this is what a variable really looks like:

bob = "blue"

(Remember "OUTPUT = INPUT"? looks alot like the written variable huh? )

There you have it... your first variable. That's all you need to write to establish a variable. Now even though this may seem like a very simple thing to do, there are alot of guidelines which govern the structure of the variable. The structure consists of three basic parts, the variable name, the all powerful equal sign, the value. We will discus the attributes of each componant in way-to-much-information detail in the following sections.

The All Powerful Equal Sign

The equal sign automatically tells flash that you are establishing a new variable. So you can see that the equal sign is an important thing in and of it's self. That's really all there is to know about the equal sign.

The Variable Name

You are free to use anything you want as the name for a variable. Usually is stick with easy to remember words.. and/or words which are fitting to the value or reason for the variables exsistance... Rules which govern the naming of variables are listed below.

It can not be:

it can be:

The Value

Now this is the tricky part... there are alot of things that you can put in this part of the variable... So i suppose i'll start off with a couple of examples, so that you kinda get a feel for what's going on here.

Let's say that we decided to set the following variable:
bob = 13
Flash will determine that the variable bob is a number.

If you decided to set the following variable:
bob = "blue hair"
Flash will determine that the variable bob is a word.

If you decided to type the following:
bob = 6 + 7
Flash will do the calculation and determine that the variable bob is number 13.

If you decided to type the following:
bob =
(without anything after the equal sign) Flash would freak out... and not know what to do.

If you decided to type the following:
bob
You wouldn't hurt anything, but Flash simply wouldn't quite understand what you are trying to do or say. Flash would look at the little word "bob" and say "huh, look, the letter's "b", "o", "b" oh well must not be important, i'll just ignore that."

Now here's the hard one:

If you decided to type the following:
bob = frank
Flash will determine that bob is equal to whatever frank is equal to... if frank doesn't exist, then either will bob! (yes wierd, but true... have you forgotten OUTPUT = INPUT already? you'll get it once you read through this section completely... just remember, we are dealing with concepts here, so it kinda has to "sink in" : )

When Flash reads a variable, it needs to determine what type (or kind) of information the variable contains - so that it will know what to do with it - should the variable's value (which is the same things as the variable it's self) be needed to perform some crazy operation in the future. The reason for this madness is because numbers can be used for mathematical functions, but words can not. Additionally, sometimes you might want to use a variable to point to another variable - or the variable might contain some long calculation, or information about a movie clip's property and so on. So in order to let Flash know what your intentions for the variable are, "the value" must be structured properly.

These are only four types of things you can possibly put into "the value" side of a variable, which we will explore in great detail below

1. "String"

Strings are words or phrases.. yup, regular old words. To specify a regular word or phrase such as "blue hair" you need to place quotes around the word or phrase.

Examples:
"blue hair"
"russian"
"has a big tatoo"

Also, you can turn a number into a "word" by placing quotes around it.
Example:
"33"

You can mix and match words, numbers and funky charactors too:
Examples:
"age 33"
"bob isn't going to ___ with this-or-that and is 100% able to spend $15 per minute!"

2. Number

Numbers can be used for mathmatical things, as well as for identiying things. Sometimes you need a number to help with a mathematical function, and sometimes you use a number to identify... to use a number as a real number, which can be used for mathematical functions just put the number in there.

Example:
33

3. Boolean

Eeek, this is a scary term. True? Well, there is really not much to it, a boolean is simply "true" or "false". Yup that's it, one or the other. A ballooean thing is written as the actual word "true" or "false" or expressed using the number "1" or the number "0". Where "1" is equivilant to "true" and "0" is false. You never use quotes to express a boolean thing. below are examples of the proper way to write boolean things:

To express True:
true
or
1

To express False:
false
or
0

NOTE: Sometimes Flash can't quite determine whether 0 or 1 is to be used as a number or a boolean value. To force the value as a number do so like the below example:

Example:

bob = Number(1)

or

bob = Number (0)

4. Expression

Okay, now we're getting somewhere. The Expression is a biggy, so hold onto your socks. An expression can be a variety of things... a calculation, another variable or combination of other variables, a movie clip property, a couple of "strings" put together, or a mix and match of all of these glopped together.

To explain this better, i'm going to stick to examples. All of the exaples listed below will be inter-related. so pay attention!

Here we have a calculation:
bob = 13 + 64
hence bob would be 77. Flash automatically does the calculation and simply thinks that bob is the number 77.

Here we are using a variable as the value of another variable:
frank = bob
So you can see that frank is the same thing as bob... and if your following along, you should recognize that frank would be equal to the number 77 - right? heck bob is 77 - so if frank is equal to bob, then frank must be 77 too.

Now let's do a little trickery.
sally = bob + 12 + frank
So here sally would be equal to... huh, hold on let me get my calculater...166. So flash would recognize sally as the number 166

Here's how we would put together a couple of strings
animal = "panther"
name = "fart"
whatever = "i have a pet " add animal add " that i call " add name
So Flash will recognize whatever as "i have a pet panther that i call fart" - See i'm getting sneaky here... let me do a little explaining... Notice that when we wanted to add together numbers we used the plus sign - but that doesn't work for strings. For strings you will need to use a "wordy" type thing to combine them together. Math type calculations use standard math-like symbols to perform the operation, strings use string-like things to perform the operation... hence these "things" i'm talking about are called operators. Here's a complete list of the operators you can use in an expression:

Math Operators  

+

Adds Numbers

-

Subtracts Numbers

++

Add's 1 to Something Automatically

--

Subtracts 1 to Something Automatically

*

Multiply Numbers

/

Divide's Numbers

 

String Operators  

add

String concatenation (formerly &)

 

Logic Operators  

<

Less than

<=

Less than or equal to

>

Greater than

>=

Greater than or equal to

==

Equal

!=

Not equal

&&

Logical AND

||

Logical OR

!

NOT

?:

Conditional

 

Sometimes is is necessary to establish a variable without specifying a value:

Example:
var poop

poop is now a recognized by Flash as a varible - "var" is a command that tells flash "hey, pay attention, i'm going to use "poop" as a variable soon, so don't freakout if it shows up in a formula or something... it's just a variable." It's kinda like telling your wife that your friend Bob might show up at the door in a couple of minutes... just so that she knows.

But remember, it is not necessary to establish a variable before assigning a value to it, you can just type it in and be done with it. like so:

poop = "terd"