Tip:
Highlight text to annotate it
X
Now we will look at assigning variable values. If you are following along, go
ahead and open up Assigning.fla. Again, if you don't have the Exercise File,
just create a blank file and rename Layer 1 to Actions. Let's select the first
keyframe of the Actions layer and opens up the Actions panel. Again, that's
Option+F9 on the Mac, or F9 on the PC. What I am going to do here is I am going to
create another variable, and so I'm going to type var and then a space and
then the name of the variable which is going to be userName, capital N,
the data type assigned with a colon is going to be String with capital S, and
again, I am typing String and once it gets highlighted in the code hinting window,
I am just going to press Return on my keyboard to have Flash complete the rest
of that word for me. Then what I am going to do is I am going to wait to set
a value, so I am going to type a semicolon right there.
Now one of the powerful things that you can do with variables is not
immediately set a value. For example, if you have a variable that represents a
user name for someone logging into a site, that user name won't be identified
by you the programmer. The user name is actually going to come from a
TextField when the user logs in, so we don't have that data until later on.
But we still going to have a container to hold that data for whenever we can
access it. That's why sometimes you create a variable, assign data type,
and don't give the variable a value just yet. And that's exactly what I am
doing right here. I am just declaring a variable called userName, telling Flash
it's going to hold a String data type, and waiting to give it a value. Now
let's look at the Syntax to give to the variable a value later on. I am going
to go down a few lines by pressing Enter or Return twice, and then I am going
to give the useName variable a value. So I am going to type userName, space, equals,
space. We are going to set it equal to, and I am just going to type todd all lower
case as my user name, and then a semicolon. You may have noticed that before I
entered the word todd, I typed open and closed quotes. I am doing that on
purpose, actually because a lot of times in programming you need things to open
and close and Strings are a perfect example of that.
Let's say I forgot to put the closed quote. If I delete the closed quote here,
you will notice that the semicolon turns green. That's because the String hasn't
terminated. So Flash thinks the semicolon is part of my String or my Text
Value. So I don't want that, so I make sure to open and close everything at the
same time. So that includes Strings, and many other things which we will talk
about in later movies. So again, all you have to do if you want to assign a
variable value in the future is declare it at one point by using the var
keyword, declaring the variable name, and the data type, and then a semicolon
to end that statement and when you want to give the variable a value, type the
variable name, and you don't have to declare the data type because Flash
already knows what data type the variable is because you have created the
variable already, and you just set the value, using equal sign, to whatever value you want.