Variables 101
Variable are essential to ActionScript. If they didn’t exist coding would be extremely hard and time consuming. In this tutorial I will be teaching you about the kinds of variables you can find in ActionScript 2.0 and what they do.
What is a Variable?
If you have never done algebra or PHP or anything involving variables before you might not know what I am talking about. A variable stores data, and in ActionScript, that data can be changed.
Say you might have a variable called ‘Hello’. ‘Hello’ might store the string ‘Hello, I am Bob’. So then instead of having to write ‘Hello, I am Bob’ over and over again you can just refer back to the ‘Hello’ variable.
Variable can be many other things, such as numbers. Pretend the variable ‘Value’ equals 5. But you might want to subtrace 2 off ‘Value’. So then ‘Value’ equals ‘3′.
To set a variable, you write it like this:
Every thing that is in caps means you have to change it. So you might have:
or
Variables are case sensitive, so ‘value; is not the same as ‘Value’. You will learn more about this below.
Strings
Strings are made up of letters or numbers, but are not a number. If you have a string that is ‘5′, you cannot add or subtract with it, because it isn’t a number but a string. When you are using strings in your code you put them in talking marks (”") like this.
or
Trace puts a message in the output panel. So if you used the code above, ‘This is a string will appear in the output window.
To make a variable a string, you do this:
If you wanted to use that string in a code, you might do this:
trace(myString);
So then the variable myString will appear in the output window when you test the movie (Ctrl+Enter).
Numbers
Numbers are basically the same as strings, but they are numbers. When you write a number, unlike strings you don’t put them in talking marks (”"). Here is an example of how to set a number as a variable:
A Number’s value can be changed, like so:
trace(myNumber);
myNumber += 5;
trace(myNumber);
The first trace would output the number 5, but the second would output the number 10 since we added 5 to ‘myNumber’.
You can also make a new variable from and existing variable, like this:
var myNewNumber:Number = myNumber+5;
So then myNewNumber would equal 10.
Booleans
A boolean can only have 2 values, true or false. To set a boolean, you would use this code:
or
Booleans are very useful if you want something to happen only when something is true, like if you wanted to trace the message “Value is equal to 5″ when value is equal to five, you could use a code like this:
var msg:String = "Value is equal to 5";
var myBoolean:Boolean = false;
if(value == 5){
myBoolean = true;
}else{
myBoolean = false;
}
if(myBoolean == true){
trace(msg);
}
That might not be the easiet way to do a code like that, but it gives you an idea on how to use booleans and the other variables we have learnt about so far.
Arrays
Arrays are going to be the last variable I teach you about, since you probably won’t come across many of the others for a while.
Arrays are different to all the variable I have you have learnt so far, since they have more than one value. Arrays can consist of many values, and each one is given a number starting at 0. You can refer to any value of your array by putting the arrays number then the values number.
>
You can replace 0 with whatever number the value is.
To set an array, you use a code like this:
If you wanted to trace ‘Blue’ from that array you would use a code like this:
That would output ‘Blue’.
That is just about all I am going to teach you in this tutorial. If you have any questions or feedback just post them below.















tnx for this tuto
no problem
hey good tutorial
good work explaining the things
is there a place on the web where I can learn actionscript?
thanks
Yea, right here
If there is something inparticular you want to learn just email me and I will do a tutorial on it
Hi and thanks for your tutorial, very good!
What I am wanting to do is to do a blog in flash for my other half (g/friend) to work on and then I import it into flash, but I really want to make it personal to the user, so, is there a way of say, if someone types in their name in an input text box, and in the blog, wherever the word ‘name’ appears their name would appear instead.
Can this be done?
Thanks if you can do this
everything from the begining:P
you see I want to make games
is it possible to make 1st person shooters on flash 8?
do I need any other programs?
I really appreciate your help thank you
Yes, it is possible to make a 1st person shooter

I might do a tutorial for it a bit later, but I have a few more I have to finish first.
Check back in a while and you might see a fight game tutorial which I have started writing.
How to use them with movie clips etc ?
There are many ways. Here is a quick example.
onClipEvent(load){
myAlpha:Number = 100;
}
onClipEvent(enterFrame){
myAlpha -= 1;
this._alpha = myAlpha;
}
If you placed that code on a MovieClip, the movieclips alpha would decrease each frame, Since the MovieClips alpha (this._alpha) is equal to myAlpha which is decreasing by 1 every frame.
Hey your tutorial is really nice thanks! I was wondering if you had a tutorial on having commands using action script. For an example with a fighter having the “a” key be punch and the “d” key being kick. Thanks!
is there any tutorial site where i can learn details of flash mx Actionscripting?
@Josh: There is one here where you can make a movieclip move left when you hit the left arrow key, right when you hit the right arrow key etc. I am making one at the moment so you can make your character kick and stuff aswell.
@Rupnarayan: If you are using MX 2004 these tutorials should still work, but if you are using MX you need to find some tutorials for actionscript 1.0, since these are for actionscript 2.0.
sir i want to build a side in flash i mean to say i want some high level tutorials … i am from pakistan and i have limited resources therefor i cant achive complete trainig of flash …..
sir i will be greatfull to you if you send me tutorials or if you tell me advance level tricks…..
i have made a project in flash but in this project there are three of four flash files have been included .. i want to build a complete side in flash … sir it is very essential for me to know about calling fram form external movie and scripts…………i hope you can understand..
thanks for reading this odd massege …
www.dubai.tornado.ae/cassels
you becomming student…………zee7_idol
Do you mean a flash site? Because I’m not really sure what a flash side is.
What exactly do you want to learn?
I’m teaching myself Flash while modifying a Flash template which I purchased. It’s at www.mcsdigitalworks.com/kauai
I’m wanting to create a cgi script at the end for the contacts page and could use some help. Would you be willing put a cgi tutorial together.
Sorry, but I really would have no idea what to do. I don’t use actionscript to do anything outside of flash, I use PHP for that.
Hello Awesty.
I really like the introduction and “tutorial” on variables, it really explains!
Good job.
Cheers!
-LFOC
awesome tutorial. very informative.
but a quick question about AS. how could i make actionscript to only run when the mouse is clicked in a certain location. i have had quite a bit of trouble trying to get it to work. but it sounds pretty simple.
and advice would be greatly appreciated.
also, great tut. will be using it quite often i am sure =P
Trace
Well you could put a movieclip of button over in the location you want and put something like:
on(release){
Put your code here
}
So every time the mouse button is released on that MovieClip/Button the code between the curly brackets will run.
Good Yeah!
thats fine i like it
Will you teach us how to make a flash movie?
Do you mean as in an animation.
hey can u e-mail me at ekusnierz@san.rr.com cause i wanna learn to make a game like adventure quest (if you have ever heard of it, if u havent goto adventurequest.com) i just wanna learn how to make the attacking thing so i dont hit a conssitant 15 damage, i need variables like 10-15 damage just like in RPG’s
TY
Can you just email me at admin[@]awestyproductions[.]com. Otherwise I forget.
I haven’t played adventure quest and don’t even plan on registering just to see the attack system.
Awesome thanks.
Thanks For this tutotials,
Can you help me?
I am new to flash. I am a web Designer and 3d Animator. Now I want to enter into the world of flash and in future I would like to go for flex. So I want to learn action script top to bottom so please help me out what should I do now.?
Well, for basic Action Script, you should know how to use if…else statements, variables, and what functions are/how to make you own.
Then just build onto that. There is an endless list to what you could learn, and not very many people know action script top to bottom.
hi awesty could you help me with a code that does this:
When you hit into another MC, a variable appears with text in it saying something, and when you move away, the text disappears. Thanks.
if(this.hitTest(SOMETHING)){
trace(”MESSAGE”);
}
dear,,,
is there any books or sites that you might help with regarding action script i’m trying to do my final year project using flash scripts like a menu and i need to pick items from the menu..
pls any help would be appreciated..
thanks
Try the flash help (F1). It is really useful. Otherwise try going to your local library and finding some books on Flash. Also, google is VERY helpful.
I don’t know if this has anything to do with this, but how could you make several text input boxes and make that text you type in appear in one text area. Is that possibe? If you could tell me thankes alot
nice tutorial!!!:D
I made this password system:
Start making 2 keyframes and place “stop();” into both of them.
Then make 3 textboxes and one button,
2 text boxes is input, and one is dynamic, set one of the input text to password, and set the var. to “pw” ,
then set the username field var. to “usern”,
and the dynamictext var. to “output”.
then insert this code into the button:
on(release){
var msg:String = “Wrong password and/or username”;
if(_root.usern == “user” && _root.pw == “pass”){
gotoAndPlay(2);
}
else{
output =(msg);
}
}
awesty roX!
@VideoKing: Yea, it is possible. Try messing around with input and dynamic text.
@djodin: Nice work.
thx =D
and videoking!
maybe this will help:
make 2 text boxes on the stage, one input tex and one dynamic text.
set variable on the input text to “inputtext”
and the dynamic to “output”.
then make a button,
an place this code on it:
on(release){
_root.output = _root.inputtext;
}
Thanks for all your help djodin!! It helped alot:) Oh yeah awesty for some reason your date is messed up. IT’s a day ahead!!! Is it just my computer??
Ah… The dates are displayed in AEST (Australian Eastern Standered Time) which is my time.
Ahhhh ok. no prob then.
for (i=0; i
You cant use less then signs ( < )
nevermind, i emailed it
It’s very useful for beginners
I staying in kharghar navi mumbai
I stay in old khar
oooookay…
thanks for your good tutorials! but i have one question on the Array section: isnt it true that if you type:
var myArray:Array = [”Red”, “Blue”, “Green”, “Yellow”]
trace(myArray[1]);
That it will trace “Red” instead of Blue?
if not, can you explain why it traces the second instead of the first colour?
Dave
No, if you wrote trace(myArray[0]); it would output Red.
The first value in and array is index 0, the second is 1, the third is 2 and so on.
awesty can u put up some tutes to use actionsript for animation and also a few mouse trick tutes
pls reply dude because i have messed up with many things but yet i cannot figure out many things and also can u teach me to make games using arrays
if(Key.isDown(LEFT)) {
_root.instance._x -= 5;
}
man, with the help of your tutes, I can now understand some actionscript :).
YOU THE MAN AWESTY!!!!!!
Hey awesty I found this site yesterday and Im hooked on it now :\ LMAO! Thanks for all the time you put into your tuts just to help us! BTW do you think you and me can team up on a tricky lil game Ive been wanting to create? Its a Medieval RPG. It has a turned base attack system and inventory and all that good stuff. You think you can help me with that? If you decide to help we will share the profits 50/50? (yes its a monthly fee game :D)
Thanks Awesty
Sorry but I don’t really use flash very much any more so I don’t think I will be able to help you make this RPG.
Getting people to pay a monthly fee for a flash game sounds pretty hard. An easier way to get money would be to get it sponsored. You can get a couple of thousand dollars for a (really good) flash game. Even if it isn’t the greatest game ever you should still be able to get a couple of hundred.
What?
You havn’t been using flash anymore?!?!
Does that mean you will stop making tutorials??? NOOOOOO!!!!
Thats too bad. I really wish you would join my team. And getting a monthly fee shouldn’t be too hard if I work my butt off making this game :/ You know Adventure Quest, Dragon Fable or any of Artix’s games? Well they charge like a $20.00 one time fee or sumthing and they have like 2 million people playing. I bet 2/5 of those people payed for it. The games Artix makes are F2P and P2P (free to play and pay to play). I think they have like 12 people on the Artix team. Any way sucks that you can’t join I really hoped you would but I guess I can find some other people to take your spot. BTW nice website =D
!
noooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooMMMAAAAAAAAAAAAAAAKKKKKKKKKKKEEEEEEEEE TTTTTTTTTTTTTTUUUUUUUUUUTTTTTTTTTTTTTOOOOOOOOOOOORRRRRRRRRRRIIIIIIIIIIIIAAAAAAAAAALLLLSSSSSSSSS
PPPPPPPLLLLLLLLLZZZZZZZZZ