Wednesday, March 3, 2010

Variables in AS2

Welcome to a tutorial on variables and how they are used in flash

This tutorial requires basic knowlage of flash and as2 actionscript

A variable is a space on you computer reserved for a set of number or letters or both

Let's look at an example:

x = y + z

Now what we have to do is change it to something flash can read

_root.x = _root.y + _root.z

All I do is put _root. in front of every variable it make it easier and less confusing

This is sometimes shown in math books and stuff what it means is "The variable x will be the sum of the variable y and x"

Now all we have to do is set the amounts for y and z we do this by putting

_root.y = 5
_root.z = 18

Or any numbers you wish in an actions tab on one of the frames on your main timeline known as the root timeline

And that is all you have created simple addition now how would we find out if it worked or not by adding this code to our root timeline

trace (_root.x)

The code trace() will make a message in our output box while testing a flash (Ctrl+Enter)

You will see the output box probably on the bottom-right corner of your screen and it will say 23 (or the sum of your numbers)

That is how you use number variables you may use variables for many things like

_root.myVar = 17;
gotoAndPlay(_root.myVar);

The code above will move the timeline to frame 17 the play(); the timeline

There are also rules for variable names

1. Two variables can not exist with the same name (they will just become one variable)
2. A variable can not start with an underscore (but they may have them in the middle or end of them)
3. A variable can not start with a number (but they may have them in the middle or end of them)
4. Varables are case sensitive so MYVAR, myvar, myVar, and MyVaR are all different variables
5. A variable can not have the same name as a command like "stop" for example

Varables may also be defind I dont't really know the reson for this

var name_of_varible:Type = value

A decaration must always began with "var" followed by the name of the variable then a colon ":" then the type (case sencenitive) like Number, String etc...

We have been talking about Number variables so far so let's talk about strings now

A string is a group of letters

_root.myName = "jeru"
trace (_root.myName)

This will output jeru this will also work

_root.frameLabel = "here"
gotoAndPlay(_root.frameLabel);

That will move the timeline to a frame labeled "here"

Now we will discuss one more type of variable called a Boolean

A Boolean can only be one of to things true or false

_root.myBool = true
_root.myBool = false

That is all it can be nothing else if you try to change it then you will get an error

var myBool:Boolean = true
_root.myBool = "jeru"

THAT CODE WILL NOT WORK because we tried to make a Boolean say something that is not true or false

Now I will show you operations in variable (don't worry we're almost done)

We have already saw how to do addition in varables theres how we will do the rest of math

_root.x = _root.y + _root.z
_root.x = _root.y - _root.z
_root.x = _root.y * _root.z
_root.x = _root.y / _root.z
_root.x = sin(_root.y)
_root.x = cos(_root.y)
_root.x = tan(_root.y)

That is how you do adding, subtracting, mutipling, and division in flash (sin, con, and tan are for triginomotry equation)

There are some differences with oporations in numbers for example I want to add 1 to a varable

_root.myVar + 1

Will not work

_root.myVar = _root.myVar + 1
_root.myVar += 1
_root.myVar++

All three of those codes will work when you put two pluses after a variable for some reason it goes up by one

There are also operations for strings

_root.firstName = "jeru"
_root.lastName = "none"
_root.fullName = _root.firstName + " " + _root.lastName
trace (fullName)

What I am saying here is fullName is my first name plus a space then my last name so it will output "jeru none"

That's all for now i hope you enjoyed and understand my tutorial if you have a questions then e-mail them to me at dante22_22@msn.com

--Sasuke2910

No comments:

Post a Comment