[AS3 Chapter 1] Variables
Areost :: Special & Secret :: Schooool
Page 1 of 1
[AS3 Chapter 1] Variables
This is part one of an actionscript 3 guide that I hope to complete some time in my lifetime before they release actionscript 4. It is supposed to be understandable for beginners. I myself am learning this while I am writing it because teaching is supposed to make learning easier (although I have learned practically all of this before, but whatever). Learn with me and if you have any questions, feel free to ask.
Variable Basics
Variables are things (lack of better words) that hold information.
There are five basic types of variables in actionscript 3. (There are actually some more, but I don't remember what they are.) Anyhow, these are all of the ones that I know.
Number
A number is any number from 4.9406564584124654 * 10^-324 to 1.79769313486231*10^308. Numbers can include decimal values (such as 2.3, 5.45 or 12490234.294303) or they can be whole numbers (2, 4, 4284 any number without a decimal). They can also be negative (-2, -65, -234.35) or positive (5, 3, 345).
Examples:
4.5666
-3354.54354
-34
3
"dog" is not a number because numbers can only be numerical values (symbols between 0-9) or decimals "." and hyphens/dashes "-".
Integer
An integer is any number from -2147483648 to 2147483647. Integers can be negative or positive, but they cannot be decimal numbers.
Examples:
4
23943
-23423
0
-2
-2.4 is not an integer because it contains a decimal. "cat" is not an integer either because integers can only contain numerical symbols (0-9) as well as the "-" hyphen/dash (in front of the number).
Unsigned Integer
An unsigned integer is and number from 0 to 4294967295. Unsigned integers cannot be negative and they cannot contain decimals. They can be thought of as integers without signs (the negative sign "-"), hence the name unsigned integers. (Well, that's what I like to think of them as. ) Since unsigned integers do not contain negative numbers, notice that the range of the numbers on the positive side is almost double that of the integers.
Examples:
24
0
248032
34.5 is not an unsigned integer because it contains a decimal.
-4234 is not an unsigned integer because it contains a hyphen (negative sign - unsigned integers cannot be negative).
"potato" is not an unsigned integer either, because unsigned integers can only contain numerical symbols (0-9).
String
A string can be thought of as a phrase or a sentence. It can include numbers, symbols and letters. To properly denote a string, double quotations are used.
Examples:
"Hello, world!"
"Hi, my name is 808."
"2432423"
"@@@@"
Technically, a string is without the quotations but you cannot use a string without the double quotes while you are coding, so I will just use them here.
Boolean
A boolean is an operator that has a value of either true or false.
Examples:
true
false
...booleans cannot be anything other than true or false. No, tru will not work.
Array
An array is a variable that works like a list, or a matrix. They can hold many values.
This will be covered in a separate post.
Declaring a Variable
Declaring a variable in actionscript 3 is very simple actually. You take the first three letters of 'variable' (var) and put it in front of the variable name, like so:
and then you have your variable! But what is the semi-colon at the end for, you ask? Well, say you have a piece of code that runs for many lines:
How will the computer know when the line has ended? Wouldn't it be troublesome if the program assumed that your code ended after 'all' in the first line? Then there would be a whole bunch of syntax errors because the program would say that you didn't end your string properly. So, we use the semi-colon to tell the computer/program (or whatever else you call it) when the line has ended.
Yay! I hope that made sense .
Anyways, back to declaring variables. The above statement
The double slashes // are used to 'comment' lines in code, meaning that anything typed after the double quotations won't do anything for the code. When the comments extend over multiple lines, a slash and an asterisk is used. /* insert some really long comment here */
You can also declare a starting value for the variable by adding the value after an equal sign. However, the value must match the variable type or else an error will occur.
Notice that in the above example, the declaration of the integer used "int" instead of integer. Well, that's the way it is and you'd better remember all the shortened versions well. Here's a list of all the proper variable declarations (of course, the "value" must be replaced with an appropriate value and the "varName" can be replaced with a name:
Naming Variables
Variables can be named to your liking, however, there are many rules to the naming of variables. First of all, variable names can only include numbers, letters, underscores (_) and dollar signs ($). However, variable names cannot start with numbers.
Examples:
_variable
variable
variable2
$variable
2variable is not acceptable, because variable names cannot start with numbers.
variable# is not acceptable, because variable names can only contain alphanumeric symbols (a-z,A-Z,0-9), hyphens _ and dollar signs $.
Please note that variable values and names are case sensitive, meaning that if your variable name starts with a capital, then you have to use a capital when calling that variable.
Constants
Constants are variables that have values that are always constant. They are useful when you want to have a variable, but never want the value to change, whether this be accidental or purposely.
Declaring a constant
Declaring a constant is like declaring a variable, but instead of the keyword "var", you use the keyword "const".
Changing the value of a variable
If you specify the variable type while declaring the variable, you cannot change the variable type later on in your code. However, you can change the value of the variable. Here is an example of this:
and there you have it, the value of the variable was changed!
Trace Function
To figure out the value of a variable in Flash, the trace() function is used. The variable name is placed between the brackets (), and the results will be displayed in the Output box.
This is very useful for debugging and a whole bunch of other purposes that I will get to later.
And that's it for variables! (well, not really. There's a whole bunch of other information about variables that I haven't touched up on, but I don't really want to do that...)
Variable Basics
Variables are things (lack of better words) that hold information.
There are five basic types of variables in actionscript 3. (There are actually some more, but I don't remember what they are.) Anyhow, these are all of the ones that I know.
Number
A number is any number from 4.9406564584124654 * 10^-324 to 1.79769313486231*10^308. Numbers can include decimal values (such as 2.3, 5.45 or 12490234.294303) or they can be whole numbers (2, 4, 4284 any number without a decimal). They can also be negative (-2, -65, -234.35) or positive (5, 3, 345).
Examples:
4.5666
-3354.54354
-34
3
"dog" is not a number because numbers can only be numerical values (symbols between 0-9) or decimals "." and hyphens/dashes "-".
Integer
An integer is any number from -2147483648 to 2147483647. Integers can be negative or positive, but they cannot be decimal numbers.
Examples:
4
23943
-23423
0
-2
-2.4 is not an integer because it contains a decimal. "cat" is not an integer either because integers can only contain numerical symbols (0-9) as well as the "-" hyphen/dash (in front of the number).
Unsigned Integer
An unsigned integer is and number from 0 to 4294967295. Unsigned integers cannot be negative and they cannot contain decimals. They can be thought of as integers without signs (the negative sign "-"), hence the name unsigned integers. (Well, that's what I like to think of them as. ) Since unsigned integers do not contain negative numbers, notice that the range of the numbers on the positive side is almost double that of the integers.
Examples:
24
0
248032
34.5 is not an unsigned integer because it contains a decimal.
-4234 is not an unsigned integer because it contains a hyphen (negative sign - unsigned integers cannot be negative).
"potato" is not an unsigned integer either, because unsigned integers can only contain numerical symbols (0-9).
String
A string can be thought of as a phrase or a sentence. It can include numbers, symbols and letters. To properly denote a string, double quotations are used.
Examples:
"Hello, world!"
"Hi, my name is 808."
"2432423"
"@@@@"
Technically, a string is without the quotations but you cannot use a string without the double quotes while you are coding, so I will just use them here.
Boolean
A boolean is an operator that has a value of either true or false.
Examples:
true
false
...booleans cannot be anything other than true or false. No, tru will not work.
Array
An array is a variable that works like a list, or a matrix. They can hold many values.
This will be covered in a separate post.
Declaring a Variable
Declaring a variable in actionscript 3 is very simple actually. You take the first three letters of 'variable' (var) and put it in front of the variable name, like so:
- Code:
var variableName;
and then you have your variable! But what is the semi-colon at the end for, you ask? Well, say you have a piece of code that runs for many lines:
- Code:
var string = "Hi my name is Rainy and I like to play and with my friends, I'll play all day. My best friend Karma has a costume box and then... we pretend to be astronauts in space. Or detectives solving a case. Pirates, knights, explorers too and then we come home again!"
How will the computer know when the line has ended? Wouldn't it be troublesome if the program assumed that your code ended after 'all' in the first line? Then there would be a whole bunch of syntax errors because the program would say that you didn't end your string properly. So, we use the semi-colon to tell the computer/program (or whatever else you call it) when the line has ended.
- Code:
var string = "Hi my name is Rainy and I like to play and with my
friends, I'll play all day. My best friend Karma has a costume box and
then... we pretend to be astronauts in space. Or detectives solving a
case. Pirates, knights, explorers too and then we come home
again!";
Yay! I hope that made sense .
Anyways, back to declaring variables. The above statement
- Code:
var variableName;
- Code:
var string:String; //this declares a variable with the name string as a String
The double slashes // are used to 'comment' lines in code, meaning that anything typed after the double quotations won't do anything for the code. When the comments extend over multiple lines, a slash and an asterisk is used. /* insert some really long comment here */
You can also declare a starting value for the variable by adding the value after an equal sign. However, the value must match the variable type or else an error will occur.
- Code:
var string:String = "contents of the string"; // declares a string named string with the value 'contents of the string'
var int:int = 2.5; // this will result in an error because integers cannot contain decimals
Notice that in the above example, the declaration of the integer used "int" instead of integer. Well, that's the way it is and you'd better remember all the shortened versions well. Here's a list of all the proper variable declarations (of course, the "value" must be replaced with an appropriate value and the "varName" can be replaced with a name:
- Code:
var varName:Number = value; // declares a number named varName
var varName:int = value; // declares an integer named varName
var varName:uint = value; // declares an unsigned integer named varName
var varName:String = value; // declares a string named varName
var varName:Boolean = value; // declares a boolean named varName
Naming Variables
Variables can be named to your liking, however, there are many rules to the naming of variables. First of all, variable names can only include numbers, letters, underscores (_) and dollar signs ($). However, variable names cannot start with numbers.
Examples:
_variable
variable
variable2
$variable
2variable is not acceptable, because variable names cannot start with numbers.
variable# is not acceptable, because variable names can only contain alphanumeric symbols (a-z,A-Z,0-9), hyphens _ and dollar signs $.
Please note that variable values and names are case sensitive, meaning that if your variable name starts with a capital, then you have to use a capital when calling that variable.
Constants
Constants are variables that have values that are always constant. They are useful when you want to have a variable, but never want the value to change, whether this be accidental or purposely.
Declaring a constant
Declaring a constant is like declaring a variable, but instead of the keyword "var", you use the keyword "const".
- Code:
const varName:Type = value; //declare a constant with the name varName
Changing the value of a variable
If you specify the variable type while declaring the variable, you cannot change the variable type later on in your code. However, you can change the value of the variable. Here is an example of this:
- Code:
var numDogs:int = 0; //declare an integer named numDogs with an initial value of 0
numDogs = 2; //change the value of numDogs to 2
and there you have it, the value of the variable was changed!
Trace Function
To figure out the value of a variable in Flash, the trace() function is used. The variable name is placed between the brackets (), and the results will be displayed in the Output box.
- Code:
var numCats:Number = 2.5; //declare a variable
trace(numCats); //output will be 2.5
numCats = 57;
numCats = 42;
trace(numCats); //output will be 42
This is very useful for debugging and a whole bunch of other purposes that I will get to later.
And that's it for variables! (well, not really. There's a whole bunch of other information about variables that I haven't touched up on, but I don't really want to do that...)
Last edited by Pukito on Sun Oct 09, 2011 4:55 pm; edited 2 times in total
Re: [AS3 Chapter 1] Variables
Incrementing Variables
Incrementing means to increase the value of something. This holds true when dealing with variables as well. You might need to increment values for a variety of reasons that you can imagine, but those will be learned later. For now, we will deal with how to increment the value of a variable. There are a couple ways to do this:
In the above examples, the number 1 was used. Of course, different numbers could have been used as long as it stays within the scope of the variable.
Decrementing Variables
Decrementing is the opposite of incrementing. It means to decrease the value of something. There are a couple of ways to do this as well. It is really the same as incrementing, but the plus + sign is replaced with a minus - sign, or dash if you prefer.
As with incrementing, the increasing and decreasing of a variable must still keep that variable within the range of its type.
Using math with variables
You can use the numeric variable types (number, integer, unsigned integer) to do math.
Mathematical operators:
Addition adds the values
sign: +
Subtraction subtracts one value from another
sign: -
Multiplication multiples the values
sign: *
Division divides one value by another
sign: /
Modulus Operator figures out the remainder after one value is divided by another
sign: %
Combined Operations
Operations can be combined. But remember that the order of operations still applies. (BEDMAS)
Incrementing means to increase the value of something. This holds true when dealing with variables as well. You might need to increment values for a variety of reasons that you can imagine, but those will be learned later. For now, we will deal with how to increment the value of a variable. There are a couple ways to do this:
- Code:
var variable:int = 0; // declare variable
variable += 1; //adds 1 the the current value of the variable
trace(variable); //output will be 1
variable = variable + 1; // adds one to the current value of itself
trace(variable); //output will be 2
In the above examples, the number 1 was used. Of course, different numbers could have been used as long as it stays within the scope of the variable.
- Code:
var variable:int = 0; // declare variable
variable += 0.5; // this would not work because integers cannot contain decimals
Decrementing Variables
Decrementing is the opposite of incrementing. It means to decrease the value of something. There are a couple of ways to do this as well. It is really the same as incrementing, but the plus + sign is replaced with a minus - sign, or dash if you prefer.
- Code:
var monies:int = 25000; //declare variable
monies -= 25; //subtract 25 from the current value of monies
trace(monies); //output will be 24975
monies = monies - 5; // decrease monies by 5
trace(monies); //output will be 24970
As with incrementing, the increasing and decreasing of a variable must still keep that variable within the range of its type.
- Code:
var monies:uint = 5; //declare variable
monies -= 6; //this will cause an error because unsigned integers cannot be less than 0
Using math with variables
You can use the numeric variable types (number, integer, unsigned integer) to do math.
Mathematical operators:
Addition adds the values
sign: +
- Code:
var number1:Number = 1;
var number2:Number = 1.5;
var number3 = number1 + number2;
trace(number3); // output will be 2.5
Subtraction subtracts one value from another
sign: -
- Code:
var number1:Number = 5;
var number2:Number = 4.3;
var number3 = number1 - number2;
trace(number3); // output will be 0.7
Multiplication multiples the values
sign: *
- Code:
var number1:Number = 10;
var number2:Number = 2;
var number3 = number1 * number2;
trace(number3); // output will be 20
Division divides one value by another
sign: /
- Code:
var number1:Number = 10;
var number2:Number = 2;
var number3 = number1 / number2;
trace(number3); // output will be 5
Modulus Operator figures out the remainder after one value is divided by another
sign: %
- Code:
var number1:Number = 10;
var number2:Number = 3;
var number3 = number1 % number2;
trace(number3); // output will be 1
Combined Operations
Operations can be combined. But remember that the order of operations still applies. (BEDMAS)
- Code:
var number1:Number = 1;
var number2:Number = 2;
var number3 = number1 + number2 * 4;
trace(number3); // output will be 9
Similar topics
» [AS3 Chapter 2] Functions
» [AS3 Chapter 5] Loops
» [AS3 Chapter 6] Event Handlers
» [AS3 Chapter 3] Array Basics
» [AS3 Chapter 4] Conditional Statements and Operators
» [AS3 Chapter 5] Loops
» [AS3 Chapter 6] Event Handlers
» [AS3 Chapter 3] Array Basics
» [AS3 Chapter 4] Conditional Statements and Operators
Areost :: Special & Secret :: Schooool
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Nov 29, 2011 10:27 am by Karma
» Hi guys! Lalala
Mon Nov 28, 2011 9:26 pm by Pukito
» ARGHHHrrhhghghg
Sun Nov 27, 2011 1:25 pm by Pukito
» [Flash Game] - I don't even
Sat Nov 26, 2011 11:08 pm by Karma
» Hi, nice to meet you.
Sat Nov 26, 2011 12:48 am by Pukito
» I think I see ..
Sat Nov 26, 2011 12:47 am by Pukito
» Where Karma goes...
Fri Nov 25, 2011 2:14 am by Karma
» Guys
Tue Nov 22, 2011 9:11 pm by Pukito
» Karmini's Pose
Tue Nov 22, 2011 8:31 pm by Karma
» The Order (1)
Sat Nov 19, 2011 10:42 pm by Karma