Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So now let's talk about setting variable datatypes. The first thing we want to do is open up a new Flash file. So let's create a new one from the Welcome screen. And let's name layer 1 actions. Click on the first frame of the actions layer and press F9 on the PC or Option+F9 on the Mac and open up the Actions panel. Now let's create that userName variable that we were talking about before. So to create a variable the first thing that you need to do is type the word var, that's var, and then type a space. Var is a keyword in Flash and notice that it turns blue when you type it.
This word says I'm going to create a variable. So after the space then you type in your variable name. So var has to always be var, your variable name can be what ever you want. Let's just say we're creating the user name for a web site. So I'm going to type of user and then Name with a capital N, and you're under the same restrictions of spaces and special characters with variables as you are with instance names and symbol names etc. Again no spaces, no special characters. Best practice, start with a lowercase letter and then each subsequent word start with a capital letter, that way it's easy to read. So userName and then type a space and equals and then a space, and let's say our user name is going to be a text value. That type of data is called a string.
A string can be any combination of numbers, spaces, special characters, all at stuff. So when you're defining a string in Flash, the string needs to be in quotes. So if the user name were to be Todd, I would type open quote and then Todd close quotes, semicolon. So userName = "Todd"; and that means it's a string of text. It doesn't have to be Todd obviously. It could be anything you want. I just made it up cause it's my name, first name I thought of. But it needs to be in quotes because it's a string. Now here is the setting the variable datatype that's very important. We were defining variables the best practice and very important thing that you can do is specify what type of data is going to be held inside of that variable. Sometimes you'll have a variable that holds numbers. Sometimes you'll have a variable that holds text or whatever.
The way you specify what type of data a variable will hold, is after the variable name, so I'm going to click right after userName, not a space after it, just immediately after the e, put your cursor right there, then type a colon and then another code hinting menu pops up. This time the code hinting menu is letting you assign a datatype for your data that your variable's holding. Because, like I said earlier, this datatype is a string, I'm going to the word string. Make sure you give it an upper case S. So uppercase S t r, Oh! And it's already highlighted. I'm going to press Return or Enter on my keyboard and Flash types out the rest for me. So we created a userName that's a string and it's equal to Todd. I could put any value I want in here. Sammy, Bob, Jimmy, doesn't matter any kind of userName inside the quotes and that will be acceptable. So why do you want to assign a datatype to a variable? Basically it will help you later on with your code, because sometimes you're going to put the wrong type of data inside a variable, and you want Flash to tell you that s o that you know that you're making a mistake in your code, and it's not something else.
And so that is how you assign a datatype to a variable and define a variable. In the next movie we're going to use something called a trace statement to check a value inside of a variable.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101705 Viewers
61 Video lessons · 88460 Viewers
71 Video lessons · 72288 Viewers
56 Video lessons · 104011 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.