00:00
00:00

AS: Conditions and Loops

5,604 Views | 19 Replies
New Topic Respond to this Topic

AS: Conditions and Loops 2005-06-28 11:47:19


AS: Main

What is a condition and why is it useful?
If you think about your every day life, you use conditional logic all the time. Example: If I have something I can contribute to the thread then I will post a reply (okay, so mayb not all of us). Similarily, Flash can test if something is true or false and then execute different commands based on that. This could be used to test if a password is correct, or if a movie clip has a certain proprety.

if()
In basic programming languages, "then" usually accompanies each and every if. In Flash, the then is implied. The if operator takes one parameter (the thing inside the parenthesis.) The parameter is a boolean (variable that can only be true or false). If that parameter is true, then Flash executes the code between the curly brackets. If its false then it doesn't. Example:

userInput = "bologna"
if (userInput == "bologna") {
trace("I could use a sandwich!");
}

This tests to see if the variable userInput is equal to the string bologna. In the above case it returns true and the line "I could use a sandwich" is displayed in the output screen. If you change user input to say something like turkey, then the sentence won't be displayed.

<else>
Else can only occur once per if statement. Else tells Flash what to do if the parameter in the if statement is false. Example:

userInput = "ham"
if (userInput == "bologna") {
trace("I could use a sandwich!");
} else {
trace("I prefer bologna.");
}

This will display the line "I prefer bologna." in the output window because "ham" doesn't equal "bologna". If you change userInput back to "bologna" then you should see the first line shown again instead.

else if ()
Else if allows you to make multipath loops. If Flash determines the initial if statement to be false then it begins testing the else ifs until it finds one that is true. Example:

grade = 75;
if (grade >= 90) {
gradeLetter = "A";
} else if (grade >= 80) {
gradeLetter = "B";
} else if (grade >= 70) {
gradeLetter = "C";
} else if (grade >= 60) {
gradeLetter = "D";
} else {
gradeLetter = "F";
}
trace(gradeLetter):

This will test if the grade is greater than or equal to the grade needed for the grade letter. If it is true, then it will assign that and skip the rest of the if/elseif/else statement. If it gets all the way down without finding an answer it will give the grade an F.

switch, case, default and break
I am lumping all of these together, because without one another they are relativly useless. When used together they can accomplish some basic things:

userInput = "bologna"
switch (userInput) {
case "bologna":
trace("my favorite!");
break;
case "ham":
trace("ham is okay, but bologna is better.");
break;
case "turkey":
trace("Yucky, go with bologna next time.");
break;
default:
trace("You must like strange sandwiches");
}

Note the colons instead of the semicolons after each case statement. You can test around with this. The break statements skip out of the switch as soon as one of the conditions is met. In this case you could remove it and there would be no noticable difference.

What is a loop and why is it useful?
A loop is a set of code that gets executed several (or even thousands of) times in a row. There will be some variable in the loop that changes each time. They are useful for hundreds of things. A few examples would be: testing your character against multiple platforms in your newest platformer, testing for multiple correct passwords, testing pratically anything that you have more than one of.

while()
While is the simplest loop there is. It's pretty much self explanatory, but I'm going to explain it anyway. While takes one parameter. This parameter is a boolean. The loop executes whatever is in the block of code while the parameter is true. Example:

myNumber = 10;
while (myNumber <=100) {
myNumber = myNumber + 5;
trace(myNumber);
}

If you copy and paste this into a frame, you will see that Flash runs through the loop until the myNumber <= 100 returns false.

do ... while
This is almost exactly like the code above with a slight difference: The loop is executed and then it checks if it should go back and do it again. So, using a similar example to above to illistrate the difference:

myNumber = 110;
do {
myNumber = myNumber + 5;
} while (myNumber<=100);
trace(myNumber);

You will see that the number is increased by 5 even though it was already above 100. This is because the do ... while loop will always be called at least once.

for()
For loops are my favorite because they are specialized while loops that I often find more use for. The for loops requires three parameters. The first is an initiation spot. The second is the condition. Third is what Flash does each time it completes a loop. For example (no pun intended):

for (x=0; x <= 100; x++) {
trace(x);
}

How Flash reads this: First Flash sets x equal to 0. It then tests x <= 100 and returns true so it runs all of the code between the curly brackets (in this case trace). After that code is done, Flash increments x (adds one to x). Flash then tests if x <= 100 again and then runs the code again and then increments and tests and so on.

Response to AS: Conditions and Loops 2005-06-28 11:53:39


Very, very useful. Nice job.


- - Flash - Music - Images - -

BBS Signature

Response to AS: Conditions and Loops 2005-06-28 11:56:51


Nice, there is a way to put in a while loop as a preloader. I'll post a thread on that.

Response to AS: Conditions and Loops 2005-06-28 12:00:10


Oh wait, my bad. There's no while loop preloader that I'm aware of, I meant to say I know a good duplicating trick with the while loop. I'll post that.

Response to AS: Conditions and Loops 2005-06-28 17:03:51


True.

Response to AS: Conditions and Loops 2005-06-28 18:37:29


Yeah, this one was very good, it gets my thumbs up ^^
Nice one.

Response to AS: Conditions and Loops 2005-07-01 03:48:34


Advanced For.... In loops

linked to our threads from eachother

Response to AS: Conditions and Loops 2005-12-12 15:28:25


At 6/28/05 11:47 AM, BleeBlap wrote: grade = 75;
if (grade >= 90) {
gradeLetter = "A";
} else if (grade >= 80) {
gradeLetter = "B";
} else if (grade >= 70) {
gradeLetter = "C";
} else if (grade >= 60) {
gradeLetter = "D";
} else {
gradeLetter = "F";
}
trace(gradeLetter):

Will this work too?

gradePercentage = "90"
switch(gradePercentage) {
case >=90:
trace("A");
break;
case >=80:
trace("B");
break;
case >=70:
trace("C");
break;
case >=60:
trace("D")
default:
trace("F");
}

Response to AS: Conditions and Loops 2005-12-12 15:41:01


Switch cases are very useful to program finite state machines, which can be used for example to encode enemy AI with changing behaviors.

Response to AS: Conditions and Loops 2006-03-18 06:56:11


is there any way to test if its an instance of a specific movieclip with for loops so if i export a MC to actionscript with the tag of Ball. i tried doing:

if(_root.[i] instanceof MovieClip.Ball){

it didnt work. is it because of my code or is the problem that it just cant be done?

Response to AS: Conditions and Loops 2006-03-18 07:20:54


At 3/18/06 06:56 AM, Flame_Reaper wrote: it didnt work. is it because of my code or is the problem that it just cant be done?

There's no built-in way to check it, to my knowledge, but you could easily make a workaround. One way is to double click the ball's icon in the library to edit it. Then let the first frame of the movie clip have the action:

var ball:Boolean = true;

Then you could simply check:

if (_root[i].ball) {
// Actions
}


BBS Signature

Response to AS: Conditions and Loops 2006-03-18 07:40:31


ahh so simple. why didnt ithink of tht.......proby cos im stupid...yes...yes thts its..

thx dude it seems to work just fine :)

Response to AS: Conditions and Loops 2006-03-18 07:44:49


At 3/18/06 07:40 AM, Flame_Reaper wrote: thx dude it seems to work just fine :)

Anytime ;)


BBS Signature

Response to AS: Conditions and Loops 2006-08-08 09:16:11


At 6/28/05 11:47 AM, BleeBlap wrote: A loop is a set of code that gets executed several (or even thousands of) times in a row. (...) A few examples would be: testing your character against multiple platforms in your newest platformer

How do you that anyway? Test your character against thousands of platforms? Ok, so say that my characters name is 'player' and my platforms are called 'floor' (or 'floor1', 'floor2', etc if neccessary), what would the code be? I need to know quick and any helpful response would be appreciated!


...

BBS Signature

Response to AS: Conditions and Loops 2006-08-08 09:58:56


About break statements: There would definitely be a noticable difference if you left the break statements out of that switch. Switches are designed so that they won't exit a case as soon as another one is encountered, meaning, without the break statement, it'll keep going down the list. In your example, that would result in multiple trace statements.

This element of switches can be useful though, like if you want the same function to execute for different values of a variable, all you have to do is stack the cases on top of each other, like so:

switch(thisVar) {
case 1:
case 2:
case 3:
trace("thisVar is either 1, 2, or 3");
break;
default:
trace("thisVar is NOT 1, 2, or 3");
break; //This one is technically unnecessary because the switch body ends here.
}

You can use that method too if you don't want something to be case-sensitive, like

case "a":
case "A":
trace("You input 'A'");
break;

Another use for breaks is early exit out of loops, like you want to cycle through an array, but only until you find an element with a certain property, you could use break to terminate the loop as soon as that element is found.

Response to AS: Conditions and Loops 2006-08-08 10:02:52


At 8/8/06 09:16 AM, cool_dood_3000 wrote:
At 6/28/05 11:47 AM, BleeBlap wrote: A loop is a set of code that gets executed several (or even thousands of) times in a row. (...) A few examples would be: testing your character against multiple platforms in your newest platformer
How do you that anyway? Test your character against thousands of platforms? Ok, so say that my characters name is 'player' and my platforms are called 'floor' (or 'floor1', 'floor2', etc if neccessary), what would the code be? I need to know quick and any helpful response would be appreciated!

You could push each floor element into an array and then cycle through that array every frame, but I find that sloppy and it soaks up processor time. What I would do is put EVERY floor element into a single movieclip, and then use a shapeFlagged hitTest at the x and y location of the player.

Response to AS: Conditions and Loops 2006-08-08 16:41:14


At 8/8/06 10:02 AM, Fat_Lifeguard wrote: What I would do is put EVERY floor element into a single movieclip, and then use a shapeFlagged hitTest at the x and y location of the player.

but that means it would only hittest a single point, which makes it kind of difficult to do walls and roofs. Any other methods?


...

BBS Signature

Response to AS: Conditions and Loops 2006-08-08 16:47:03


It tests a single point, but it is testing to see whether it hits any wall or roof.

Response to AS: Conditions and Loops 2007-01-15 09:05:54


Very useful information, thx!

Response to AS: Conditions and Loops 2007-01-15 13:14:26


I think that switch is better for many conditions because once it finds the correct case, it immediately executes the command. If...then on the other hand, needs to test every single line of the statement until it executes one particular statement.

Is that true?