Best viewed in Firefox

Awesty Productions

Variables 101

September 18th, 2006 by awesty

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:

var NAME:TYPE = VALUE;

Every thing that is in caps means you have to change it. So you might have:

var Hello:String = "Hello, my name is Bob";

or

var value:Number = 5;

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.

"This is a string"

or

trace("This is a string");

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:

var myString:String = "This is my string";

If you wanted to use that string in a code, you might do this:

var myString:String = "This is my first string!";
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:

var myNumber:Number = 5;

A Number’s value can be changed, like so:

var myNumber:Number = 5;
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 myNumber:Number = 5;
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:

var myBoolean:Boolean = false;

or

var myBoolean:Boolean = true;

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 value:Number = 5;
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.

trace(myArray[0]);

>
You can replace 0 with whatever number the value is.
To set an array, you use a code like this:

var myArray:Array = ["Red", "Blue", "Green", "Yellow"]

If you wanted to trace ‘Blue’ from that array you would use a code like this:

trace(myArray[1]);

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. :)

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed | Trackback URI

57 Comments »

Comment by GunMan
2006-09-22 06:12:49

tnx for this tuto

 
Comment by awesty
2006-09-22 11:40:57

no problem ;)

 
Comment by samexon
2006-09-23 21:46:52

hey good tutorial
good work explaining the things

 
Comment by Johny
2006-09-24 18:00:44

is there a place on the web where I can learn actionscript?

thanks

 
Comment by awesty
2006-09-24 22:57:08

Yea, right here :D

If there is something inparticular you want to learn just email me and I will do a tutorial on it ;)

Comment by chris Subscribed to comments via email
2007-05-02 02:00:52

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 :-)

 
 
Comment by Johny
2006-09-25 05:45:05

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

 
Comment by awesty
2006-09-25 16:30:44

Yes, it is possible to make a 1st person shooter :D
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. ;)

 
Comment by vishal
2006-09-25 17:16:04

How to use them with movie clips etc ?

 
Comment by awesty
2006-09-25 19:30:34

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.

 
Comment by Josh
2006-09-27 03:40:01

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!

 
Comment by Rupnarayan
2006-09-27 05:55:50

is there any tutorial site where i can learn details of flash mx Actionscripting?

 
Comment by awesty
2006-09-27 10:43:53

@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.

 
Comment by Zee
2006-09-27 14:50:31

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

 
Comment by awesty
2006-09-27 20:04:21

Do you mean a flash site? Because I’m not really sure what a flash side is.
What exactly do you want to learn?

 
Comment by Michael
2006-09-29 11:09:21

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.

 
Comment by awesty
2006-09-29 13:26:26

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.

 
Comment by Luis Felipe
2006-09-30 03:31:32

Hello Awesty.

I really like the introduction and “tutorial” on variables, it really explains!

Good job.
Cheers!
-LFOC

 
Comment by Trace
2006-10-04 13:37:55

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

 
Comment by awesty
2006-10-06 17:17:56

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. ;)

 
Comment by Hashu
2006-10-10 00:48:25

Good Yeah!

 
Comment by kailash
2006-11-08 03:37:02

thats fine i like it

 
Comment by timmy
2006-11-21 22:07:47

Will you teach us how to make a flash movie?

 
Comment by awesty
2006-11-23 15:57:33

Do you mean as in an animation.

 
Comment by Bryce K :P
2006-12-01 10:58:33

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

 
Comment by awesty
2006-12-01 16:26:40

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.

 
Comment by Daniel
2006-12-08 10:14:08

Awesome thanks.

 
Comment by Pritesh Gajjar
2006-12-14 04:59:35

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.?

 
Comment by awesty
2006-12-15 12:10:28

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.

 
Comment by jake02
2006-12-25 00:55:43

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.

 
Comment by awesty
2006-12-29 11:05:31

if(this.hitTest(SOMETHING)){
trace(”MESSAGE”);
}

 
Comment by nabil
2007-01-10 06:28:06

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

 
Comment by awesty
2007-01-13 15:51:01

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.

 
Comment by Videoking
2007-01-21 23:21:05

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 :)

 
Comment by djodin
2007-01-26 07:50:57

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!

 
Comment by awesty
2007-01-26 11:25:59

@VideoKing: Yea, it is possible. Try messing around with input and dynamic text.

@djodin: Nice work. ;)

 
Comment by djodin
2007-01-27 04:12:53

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;
}

 
Comment by Videoking
2007-01-27 07:21:01

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??

 
Comment by awesty
2007-01-27 12:24:24

Ah… The dates are displayed in AEST (Australian Eastern Standered Time) which is my time.

 
Comment by Videoking
2007-01-27 23:38:14

Ahhhh ok. no prob then.

 
Comment by tim
2007-02-01 00:19:35

for (i=0; i

 
Comment by awesty
2007-02-01 21:12:02

You cant use less then signs ( < )

 
Comment by tim
2007-02-02 11:30:19

nevermind, i emailed it

 
Comment by Yogesh
2007-02-09 22:32:28

It’s very useful for beginners

 
2007-02-09 22:35:07

I staying in kharghar navi mumbai

Comment by Eric Subscribed to comments via email
2007-08-21 19:08:53

I stay in old khar

 
 
Comment by awesty
2007-02-10 10:43:03

oooookay…

 
Comment by Dave Subscribed to comments via email
2007-05-05 04:20:21

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

Comment by awesty
2007-05-14 17:39:40

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.

 
 
Comment by Eric Subscribed to comments via email
2007-08-21 18:39:13

awesty can u put up some tutes to use actionsript for animation and also a few mouse trick tutes

 
Comment by Eric Subscribed to comments via email
2007-08-21 18:48:23

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;
}

 
Comment by Uzi
2007-08-27 06:34:41

man, with the help of your tutes, I can now understand some actionscript :).

YOU THE MAN AWESTY!!!!!!

 
Comment by CrazeyZaia
2008-01-15 07:04:24

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

Comment by awesty
2008-01-15 19:28:06

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.

Comment by EagleVision
2008-02-12 06:04:59

What?
You havn’t been using flash anymore?!?!
Does that mean you will stop making tutorials??? NOOOOOO!!!!

 
 
 
Comment by CrazeyZaia
2008-01-16 09:04:04

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

 
Comment by flash begginer-imidiete
2008-10-11 15:12:07

! nooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

MMMAAAAAAAAAAAAAAAKKKKKKKKKKKEEEEEEEEE TTTTTTTTTTTTTTUUUUUUUUUUTTTTTTTTTTTTTOOOOOOOOOOOORRRRRRRRRRRIIIIIIIIIIIIAAAAAAAAAALLLLSSSSSSSSS

PPPPPPPLLLLLLLLLZZZZZZZZZ

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.