|
Introduction to Variables
In a typical scripted movie, we have to manipulate everything from frame
numbers,to movie clips,to buttons. In order to retrieve all the informationm
we need to store it into variables. The primary information storage
of Actionscript.
If you have taken algebra in math then you have written variables before.
Let say y=x+3. "Y" will hold the value of 5. In programming
terms, it will work the same way text=hi. When your proramming a project
and you dont want to write down hi 50 times all you have to do is call
in text and it will right Hi for you.
Variable Declaration
Creating a variable is called declaration. Declaration is like
giving the variable a name. It can also be bringing the variable into
existence. When a variable is declared i.e: var, it is empty. To close
a variable you must declare a special value called undefined.(indicating
the absence of data).
var text;
var movieclip;
var animation;
The word var tells us that were declaring a variable. Text follows the
variable i.e: text, movieclip, and animation.
Variable Creation
Many languages require variables to be declared before data is deposited
into them. Failure to do so will result in an error. If we assign a value
to the variable that doesn't exist. It will automaticly creat one for
you. However, doing so will make your code confusing and hard to read.
Legal Variables
ItMust be writtin exclusively of letters, numbers, and underscores.
*hint
No spaces, hypens, or punctuation.
It Must start with a letter ot an underscore
It must not exceed 255 characters.
Variables are case-sensitive
Legal Variables
var computer;
var counter:
var middle_name
Illegal Variables
var 1stplace
var my name
var 2-dimes
Dynamically Assaigned NamedVariables
Even though you will never use dynamically assaigned variables. You are
still able to use it.
To create a variable name from any expression use the set statement.
To show you how to use the set statement we will assign the value
of "The Great1" to player1name
var i = 1
set ("player" + i + "name", "The Great1")
Arrays and objects, discussed later should be used instead of dynamic
variables
Declare Variables at the Outset
It is very good to practice declaring your variables at the the beginning
of your movie script, which is the first keyframe right after the preloader.
Make sure you add a comment explaining each variables purpose. So your
code won't get confusing.
// ^^^^^^^^^^^^^^^^^^^^
// Initialize variables
// ^^^^^^^^^^^^^^^^^^^^
var ballSpeed;// Velocity of ball, max 5
var score; // Player's current score
var hiScore; // High score (not saved between levels)
var player1; // Name of player 1, supplied by user
We can give variables an initial value at the same we create them
var ball speed = 5 ; //Velocity of ball, max 5
var score = 0 //Player's current score
var hiScore = o; //Hi Score(not saved between levels)
|