ActionScript Programming/PartII/Chapter 4

Conditions/Loops (part 1)
Conditions and loops are one of the most important things not only in ActionScript but also in all programming languages, because they are used everywhere. For understanding these conditions lets discuss each of them one by one.

{| border="0" cellpadding="4" cellspacing="0" style="margin: 1em 1em 1em 0; border-collapse: collapse; font-size: 95%;"
 * [[Image:mfas_image019.jpg]] ||
 * [[Image:mfas_image019.jpg]] ||

If, else if, else
“if” is used to compare two or more expressions. For example you have an “Input Text” and a button. You want to check if the value of “Input Text” equals “hello”. To do that you must use “if”. The use of “if” is the following:

The code in “if” will be executed when the condition which you write is true. But for example you wrote a condition which was false, and you want to know about it. In this case you can use “else”. But before using any condition you must know comparison and logical operators.
 * }

Operators (part 2)
Comparison operators

Logical operators

Miscellaneous operators

Now when you know these operators we can continue learning conditions and writing new programs.

Conditions/Loops (part 2)
Lets write a program which will check if the values of two Input Texts are the same. First of all create two Input Text and set first ones var to “input1” and the second ones to “input2”. Then add a button and set its name to “button”. Add the following lines to the actions of “button1”:

In line 2 we check if input1 equals input2 using “if” and operator “==” (Note: don’t confuse “=” with “==” because they are different operators. “=” assigns value to the variable, but “==” only checks two expressions. For example if you have “if” and you want to check if any variable “a” equals “hello” then you must write “ a==”hello” ” in the condition. If you write “ a=”hello” “ then a will become “hello”.) and if so then we trace “YES” else we trace “NO”. But now imagine another situation. You have an Input Text and a button and when the user type his/her name in the Input Text and clicks the button you want to trace different values. To solve this you must add the following code to the button:

If you want you can write a simple program just for you. You can create two Input Texts one button, a Dynamic Text (that will show the status) and write a code to determine if the user who want enter your program is member or not. This little program will be your security, keeping other people away. Below is shown a little fragment of the button’s actions:

UserName is the “var” property of the first Input Text Password is the “var” property of the second Input Text Status is the “var” property of the Dynamic Text

For
“For” is used to repeat the code for several times. Below is shown the use of “For”:

Now I will bring you example where you need to use “For”. For example you want to add all numbers from 1 to 10 (1+2+3+4+…+10). For doing it you have to write the following code:

1.  var i = new Number; 2.  i += 1; 3.  i += 2; 4.  i += 3; 5.  i += 4; 6.  i += 5; 7.  i += 6; 8.  i += 7; 9.  i += 8; 10. i += 9; 11. i += 10;

(or if you are too smart)

But, anyway, it’s boring to write such a code. Instead you can use “For” and repeat the code for 10 times:

The second part of “For” tells Flash how much to repeat the action. In our case we wrote to repeat the action while e is less than or equal to 10. So when “e” becomes 11 it will stop repeating the action. In the third part of “For” is you must tell Flash how to change the value of the variable each time it repeats the action. In our case we wrote “e++”. This means that each time Flash repeats the action it will increment e by one. In the actions of “For” (line 4) we wrote “i += e;”. This means that we will add “e” to “i” each time the action repeats. So this small code will give the sum of numbers from 1 to 10. You can trace the value of “i” at the end of the code to see its value. It must be “55”.

This example does the same as the previous one, but this time we increment the value of “e” by 2, not by one, as in the previous example. So our example will count the sum of the following numbers: “1, 3, 5, 7, 9”.

For .. In
“For .. In” is used to loop through the properties or objects of the object you specify. The following is the structure of the statement:

Now lets understand what, where and how can this statement be used. For example, if you want to get the list of objects in “_root” object you must write the following:

If you write this code in the actions of the frame and run it, the output value will be:



The first line of the code declares a new string variable named “e”. Then the program loops though each element in “_root” object and traces its name. You already know that each local variable declared in timeline is an element of “_root” object like “e”, but what is “$version”? “$version” is a built-in string variable which keeps the name and the version of the operating system the movie is run at.

« Previous &emsp;&emsp; Next »