Variables


Variables

The most important thing in programming is the ability to store and manipulate data. Data is simply some space in the computer's memory where letters and numbers can be kept. These spaces are called variables , and all you have to do to use them is give them a name .

For example, let's say we have a game in which the player fights monsters with his trusty sword. The player is given 100 life points at the beginning of the game, and each time a monster hits the player, his life points decrease by 5. When the player's life points reach 0, he dies and his game is over. It would be simple enough to write script to handle the player's life, but to do so, we need to keep track of those life points throughout the entire game. To do this, we would use a variable.

The simplest and most common way to create a variable is to simply refer to it. In other words, to give our player some life points, all we need to do is add some script that will execute once each time the player starts a new game. The script would include some code similar to the following:

 life = 100; 
Tip  

Try to give variables good names that reflect the data they are to hold. By naming our variable life , we can easily recall its name, and if we later see it in some of our old script, we know exactly what data it represents.

Because we have never used the label life before in our script, when Flash sees that, it creates a new variable named life and assigns it the value of 100. We can then use that same variable later by simply typing in its name.

If we want a piece of script that handles the player getting hit by monsters, we could write some script that is executed once each time the player gets hit, such as the following:

 life = life - 5; 
Caution  

You must follow rules when you name variables. All variables must begin with a letter (A “Z or a “z). They can contain only letters (A “Z or a “z), numbers (0 “9), and the underscore character (_).They must not be keywords. (Keywords are explained in a section coming up called "Keywords.")

This reduces the value of the life variable by 5. Because this script would be set up to execute once each time the player was hit, it would properly keep track of our life points. We could then go on to check if life goes below 1; if it does, we could run another piece of script that handles the player's death.

Note  

Although most of the time our data is in the form of variables, we can also type values directly into our script as in our previous example. We subtract the value 5 from our life variable by simply typing the number 5.

You might have noticed in that last piece of script that we use the minus sign to subtract 5 from the variable. Variables would be useless if we were unable to fiddle with their contents. The way in which we manipulate variables is the subject of our next section.

An important addition to Flash MX 2004 is that variable names are now case sensitive. Previously, variables named myVar and MyVar referred to the same variable. Now these are two completely different variables.




Macromedia Flash MX 2004 Game Programming
Macromedia Flash MX 2004 Game Programming (Premier Press Game Development)
ISBN: 1592000363
EAN: 2147483647
Year: 2004
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net