What are variables and how do i use them?
posted: 04-02-06
author: PrimeVector
website: http://www.free-webmaster-resource.com
views: 4773
comments: 0
total pages: 1
Check This Amazing Traffic Generating Widgetsl 4 Webmasters!
What are variables and how do i use them?
If you just entered this whole world
of codes and programming, then you might be intimitated by all those terms that are used daily in
those area's and thus in most tutorials.
In this tutorial i want to teach you a bit about one of the most basic things in coding
that you will without a doubt
be using yourself all the time. Its variables.
Variables are like little containers to remember and store things for us.
So lets say i wanted to store my name in a variable then in Actionscript i would just do this:
var name = "PrimeVector";
'var' means that we are about to declare a variable, in our case called 'name'.
This variable is then filled with the string "PrimeVector".
To keep things clean you need to place a ';' at the end of each line of code.
At this point you have a filled variable and could start doing some stuff with it.
Like if you wanted to trace an output to the screen if ran inside Flash then you could place
this line of code under it.
trace(name);
'trace' is a very useful function in Flash and should not be underestimated. With it you can check if things
are going like they should be at or in this case check if our variable called 'name' is actually filled with the string
"PrimeVector". If you would run this code then you would see a trace output on your screen displaying the contents of
variable name ... in our case it would show "PrimeVector"
Be aware
Never give names to your variables starting with a number. So '4enemy' is a bad name, but 'enemy4' is perfectly legit.
Also never give them names that are already in use by Flash itself, for example 'button'.
When you make up names for your variables try to keep them descriptive and not very crypic.
Like for example when you have a enemy in a game and you want to create some variable describing
how many bullets are left on that enemy, then you should setup a variable like 'enemyBulletsLeft' or something
along those lines. You should not make it cryptic looking like 'ebl' or other non descripive names like that.
This is because when your codes start to grow you will create some nice quite un-readable codes for yourself
if you keep naming your variables very un-descriptive.
Some variable magic
Lets say you wanted to use variables to store numbers and results of
a mathmatical process like addition. Lets look at the example below:
var numberA = 5;
var numberB = 5;
var additionResult = numberA + numberB; //additionResult would be 5 + 5 = 10
Here is the same code but with a few traces to check whats going on!
var numberA = 5;
trace("The content of variable numberA is:" + numberA);
var numberB = 5;
trace("The content of variable numberB is:" + numberB);
var additionResult = numberA + numberB;
trace("numberA added to numberB = " + additionResult);
If you would copy and paste the above code into a flash file and run it, then variable 'numberA' will be
added to variable 'numberB' and the result is placed in another variable called 'additionResult'.
So in the end variable 'additionResult' will have a value of 10.
We again declare the variables with 'var', we name it and fill it.
As you can see this opens a lot of possibilities. You cant even really do anything without the use of variables.
I also put in a few traces so you can see how the code is trigged. From top to bottom and in the end the numbers you wanted
are added and the result stored.
Conclusion
I hope this little exercise in variables could help you on your way. If you have any questions please don't be shy
and post a message in the forum.
Warning: include() [
function.include]: URL file-access is disabled in the server configuration in
/home/freeweb/public_html/tutorials/flash/basics/tutorial005/index.php on line
312
Warning: include(http://...@SF_random.php) [
function.include]: failed to open stream: no suitable wrapper could be found in
/home/freeweb/public_html/tutorials/flash/basics/tutorial005/index.php on line
312
Warning: include() [
function.include]: Failed opening 'http://...@SF_random.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php:/usr/lib/ZendGdata/library') in
/home/freeweb/public_html/tutorials/flash/basics/tutorial005/index.php on line
312
Amazing Webmaster Widgets
Comments
sorry no comments were found, you could be the first to leave a comment !
- Press here to view all available smilies!