//Steve Rhoades //Sample Code //Art Institute of Seattle //FirstLoop package{ import flash.display.*; public class FirstLoop extends MovieClip{ //constants are a great way to keep your code manageable //they cannot be changed, and allow you to define commonly used values in an easy to find place const VERY_HIGH:String = "Excellent"; const HIGH:String = "Great"; const PRETTY_HIGH:String = "Good"; const NORMAL:String = "Average"; const PRETTY_LOW:String = "Sub-Par"; const LOW:String = "Poor"; const VERY_LOW:String = "Abysmal"; public function FirstLoop(){ var stat:uint; var statDescription:String; //look you can declare multiple variables in one line var die1:uint, die2:uint, die3:uint; /** *This program has changed very little since the last one. The only difference is that in this *program, we're going to generate 6 different stats for our user (again, as if this were D&D). * *To do this, we will use another of the programmer's most basic tools: a loop! *There are two types of loops we'll use in this class, today we'll start with a while loop *It takes the form: * *init *while(condition){ * //stuff to repeat * //somewhere in here, we potentially change the condition. *} * *Here's an example that will loop for 6 stats */ //we have 6 stats var numberOfStats:uint = 6; //count will count through the six of them, programmers ALWAYS count starting from zero var count:uint = 0; //as long as we haven't counted all six yet, keep going while(count < numberOfStats){ die1 = Math.floor(Math.random() * 6) + 1; die2 = Math.floor(Math.random() * 6) + 1; die3 = Math.floor(Math.random() * 6) + 1; stat = die1 + die2 + die3; //now let's look at the if else statement if(stat >= 17){ statDescription = VERY_HIGH; }else if(stat >= 15){ statDescription = HIGH; }else if(stat >= 13){ statDescription = PRETTY_HIGH; }else if(stat >= 9){ statDescription = NORMAL; }else if(stat >= 7){ statDescription = PRETTY_LOW; }else if(stat >= 5){ statDescription = LOW; }else{ statDescription = VERY_LOW; } trace("Your stat number " + count + " of " + stat + " is " + statDescription); //this is a shorthand way to add 1 to a variable count++; } } } }