Be a Supporter!

AS: Conditions and Loops

  • 4,745 Views
  • 20 Replies
New Topic Respond to this Topic
BleeBlap
BleeBlap
  • Member since: Mar. 8, 2005
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
AS: Conditions and Loops Jun. 28th, 2005 @ 11:47 AM Reply

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.

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Conditions and Loops Jun. 28th, 2005 @ 11:53 AM Reply

Very, very useful. Nice job.


- - Flash - Music - Images - -

BBS Signature
Siggy0501
Siggy0501
  • Member since: Jan. 8, 2005
  • Offline.
Forum Stats
Member
Level 23
Blank Slate
Response to AS: Conditions and Loops Jun. 28th, 2005 @ 11:56 AM Reply

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

Siggy0501
Siggy0501
  • Member since: Jan. 8, 2005
  • Offline.
Forum Stats
Member
Level 23
Blank Slate
Response to AS: Conditions and Loops Jun. 28th, 2005 @ 12:00 PM Reply

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.

Taylor
Taylor
  • Member since: Aug. 19, 2003
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Conditions and Loops Jun. 28th, 2005 @ 01:03 PM Reply

At 6/28/05 12:00 PM, _Siggy_ wrote: 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.

You could use a while loop to put together a preloader... just do something like

while(!totalframes == framesloaded) {
//not loaded
} else {
play the movie
}

Siggy0501
Siggy0501
  • Member since: Jan. 8, 2005
  • Offline.
Forum Stats
Member
Level 23
Blank Slate
Response to AS: Conditions and Loops Jun. 28th, 2005 @ 05:03 PM Reply

True.

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Conditions and Loops Jun. 28th, 2005 @ 06:37 PM Reply

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

Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Conditions and Loops Jul. 1st, 2005 @ 03:48 AM Reply

Advanced For.... In loops

linked to our threads from eachother

Fire
Fire
  • Member since: Oct. 29, 2003
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to AS: Conditions and Loops Dec. 12th, 2005 @ 03:28 PM Reply

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

Newsdee
Newsdee
  • Member since: Jan. 21, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to AS: Conditions and Loops Dec. 12th, 2005 @ 03:41 PM Reply

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

Flame-Reaper
Flame-Reaper
  • Member since: Nov. 16, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Conditions and Loops Mar. 18th, 2006 @ 06:56 AM Reply

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?

Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Conditions and Loops Mar. 18th, 2006 @ 07:20 AM Reply

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
Flame-Reaper
Flame-Reaper
  • Member since: Nov. 16, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Conditions and Loops Mar. 18th, 2006 @ 07:40 AM Reply

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

thx dude it seems to work just fine :)

Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Conditions and Loops Mar. 18th, 2006 @ 07:44 AM Reply

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

Anytime ;)


BBS Signature
edit-undo
edit-undo
  • Member since: Jan. 17, 2006
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to AS: Conditions and Loops Aug. 8th, 2006 @ 09:16 AM Reply

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
Fat-Lifeguard
Fat-Lifeguard
  • Member since: Jul. 23, 2006
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to AS: Conditions and Loops Aug. 8th, 2006 @ 09:58 AM Reply

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.

Fat-Lifeguard
Fat-Lifeguard
  • Member since: Jul. 23, 2006
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to AS: Conditions and Loops Aug. 8th, 2006 @ 10:02 AM Reply

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.

edit-undo
edit-undo
  • Member since: Jan. 17, 2006
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to AS: Conditions and Loops Aug. 8th, 2006 @ 04:41 PM Reply

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
Fat-Lifeguard
Fat-Lifeguard
  • Member since: Jul. 23, 2006
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to AS: Conditions and Loops Aug. 8th, 2006 @ 04:47 PM Reply

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

939distrikt
939distrikt
  • Member since: Jan. 13, 2007
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS: Conditions and Loops Jan. 15th, 2007 @ 09:05 AM Reply

Very useful information, thx!

deltatsunami
deltatsunami
  • Member since: Dec. 31, 2005
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Conditions and Loops Jan. 15th, 2007 @ 01:14 PM Reply

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?