cancel
 

 

Job type :

Zip Code :

community forums

Featuring over 100 topics of interest to DoItYourselfers.

Flash Professional 8 - How to Work with String and Number Variables

AlertThis content requires Flash

To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

Download the free Flash Player now!

Get Adobe Flash Player

Video Transcript

So, we see how that access properties on several types that own it is on our flash movies already. We have got a lot more to come, but before we leave this lesson I want to get a few other things out that we consider fundamentals to understanding action script.

Now, the first to these fundamentals has to do with those variables we are talking about before. So, let us go back and take a quick look at variables again.

Now, I am going to add a few more trace statements. So that we can see our new trace statements, I am going to comment out all the old ones. I will just put slashes in front of them on that way they will not be executed and our output window will be filled up with all kinds of junk.

Okay. So, I think we have clean all of the mount there, but one more we will trace the score only that when they are for right now. Now, at the bottom of our code, I am just going to add another line and I would like to take a quick look at variables and what I am looking at now is what variable store.

Now, remember we had the variable called Fred all the way to top and we set it equal to two. Now, I would like to do a little trace statement and inside of the trace statement message I would like to perform a little addition. We can say, FRED+FRED.

Now, I am sure everyone out there can figure out what FRED+FRED is knowing that Fred is equal to two. It should be four, correct?

Alright, let us take a look, we can see place holder is up here where we test our movie and we can see the number four. So, wonder of wonders action script can actually add two numbers together and we can do almost any normal operation that you would expect to program language to do with that value.

Let us create another variable. Let us call this variable George, no I am just making up funny names for variables right now. But I would like to set George equal to two as well, but this time I am going to put the two in quotes.

Now, what I would like to do is take a look of what we are talking about when we talk about a literal string. The word string is used a lot in programming languages that basically refers to a character.
Now, how this different from Fred? Fred was set equal to let me scroll all the way up here at the top, the number two. We had two in together we can get four. When we set a variable equal to a string, this is simply the character too.

Now, let us do the same thing. Let us do a quick trace, we will do George+George. Now, the results might surprise you because we are still adding two in two together, right? Let us see what we get. I will test my movie control enter or command enter. I got 22 seems on at first, but it is actually correct. I took the character two and added onto the end of it another character two.

Now, this is going to be very important the difference between strings and numbers in the program and it is so important that I am going to rewrite the way we do our variables.

Now, you can see how this can be very confusing for you later on. In later lessons, we will be hooking up on external data sources. You might not even know the values before your flash movie runs. So, it can get even more confusing when you do not even know the values of the variables that are being dropped into this location.

Now, to fix this, I am going to introduce one more thing for describing variable in the program. We are going to do two things when we set up our variables instead of just setting them equal some value, we are also going to do typing and scoping of our variables.

Now, this is going to add to our syntax a little bit. This is a best practice in order to setup a variable, the first thing I will probably be using is the var key word. Let me change George so that we are using the var key word. I simply type var in front of it and what that does is it sets up the places where the George variable is going to be valid.

And that does not make too much sense right now, but I am introducing it as a syntax element and this is basically what we calling flash action script are best practice. It is something that you want to get use to doing.

Now, obviously you can get away without doing it because we successfully set the George variable to the character two before and without using this. But by doing this, it is setting that variable so that the variable George is only valid in this script window.

In later, more complicated scripts, let us say you are using a more common variable name like X. If you are use the variable X in one script window and use the variable X again in another script window, it can actually cost problems. You can have those two variables colliding with one another.

Okay, so, scoping is simply typing var in front of it. Now, we will be revisiting this, but I am going to add one more thing on to this and this is called Typing a Variable. I am going to use a colon at the end of George and what I am going to do is I am going to choose from one of the different types of variables like it have.

Now, we are seeing another level of hinting coming into the action script the soon as type the colon, it popped up a menu and these all are the different types of variables that I can have on the program.

If I just stick to the two that we have been working with, you can see there I can specify number if I wanted to it is be a simple numeric value like 350 right up here setting equal to the Y or I could go down here and choose string. That would set it up to be a character value the exact way what we had it here.

Now, to use this help system, you can either start typing or you can select it from the pull down menu and when we move my menu up here a little bit then you will notice as I start typing capital ST you can see it goes down to string and I can simply hit the enter key and it will finish typing for me.

Now, what we have done is we are just specified that the new variable called George will always have a string character in it. This will make sure that the program knows exactly what we are trying to put into that variable and do not get confuse is to whether we are using numbers or strings.

To use the same correct syntax on Fred, I will go back up to Fred here at the top and I had write in var using that var key word in front of Fred and I will also type it. I am starting to type NU and you can see it goes to number.

Now, since the program is case sensitive, it is very important that I type capital NU and not lower case and you can see it is not finding anything in the help menu and that gives me a good cue that I am not putting in the right values there.

Now, just to see how this is going to help me in the program if you accidentally put the wrong type of variable into something that specified to be a number. For instance, what if I would put quotes around this to value here and we test out our program, control enter. I will get an error and it says, type mismatch. It was expecting in number and I gave a string.

Now, once again, this is a little early to completely understand what we are doing, but what we are trying to do with this point is to setup some good habits for everyone because they will not have so many problems with your programming when you get into the more complex scripts.
So, we see how that access properties on several types that own it is on our flash movies already. We have got a lot more to come, but before we leave this lesson I want to get a few other things out that we consider fundamentals to understanding action script. Now,... click to read more


Painting Stylish Wall Borders

sponsored articles of the day

diy centers

Research and explore a wealth of wisdom on these topics