roll_btn.addEventListener(MouseEvent.CLICK, roll); function roll(evt:MouseEvent) { var r1:int; var r2:int; r1 = Math.floor(Math.random() * 6 + 1); r2 = Math.floor(Math.random() * 6 + 1); d1.gotoAndStop(r1); d2.gotoAndStop(r2); }
1. actions layer - each keyframe has one ActionScript statement: stop(); 2. faces layer - each keyframe has a colored face with enough pips to sing its song.
1. var r1:int; This statement creates a variable. A variable is a chunk of memory, an area in RAM. RAM is an acronym for Random Access Memory. r1 is the name of the variable. int is the TYPE of data that Flash ActionScript 3.0 will allow you to store in the r1 memory location. int stand for integer. -22, 0, 33, 17, 5, 14, 8, 21 and 10 are some examples of INTEGER int values. 2. r1 = Math.floor(Math.random() * 6 + 1); This statement results in a random number where random number r1 is >= 1 and <= 6. r1 will be 1, 2, 3, 4, 5 or 6, i.e. one of the six integers in the set {1, 2, 3, 4, 5, 6}. Do NOT worry at all about the above statement that we did in lab! We will talk about Math.random() and Math.floor() later on this summer, as needed. Obviously, there could be very few interesting computer games without random numbers, just like there would be not nearly as many board games like Monopoly and Clue and Yahtzee if it weren't for the random result you get when you roll a pair of dice! :-) Math.random() 3. d1.gotoAndStop(r1); This statement goes to frame # r1 of the dieSymbol timeline, which displays the 1 or 2 or 3 or 4 or 5 or 6 face of the die d1. d1.gotoAndStop(6); would go the the face that shows you rolled a 6, for example.
function evenOrOdd(n1:int):String { if (n1 % 2 == 0) { <-- n1 % 2 == 0 is either TRUE or FALSE return "Even"; ----------- } else { return "Odd"; } } var n1:int = 97 % 10; <------- remainder is 7 quotient is 9 var n2:int = 43 % 10; <------- remainder is 3 quotient is 4 var n3:int = 16 % 10; <------- remainder is 6 quotient is 1 var n4:int = 90 % 10; <------- remainder is 0 quotient is 9 var n5:int = 7 % 10; <------- remainder is 7 quotient is 0 n1 = 25 % 2; the mod, the remainder is 1 for ALL ODD numbers. n2 = 5 % 2; n2, a chunk of computer memory for storing int's, would be storing 1, since the remainder (MOD) when you divide 5 by 2 is 1. 5 is an ODD number, so the remainder has to be 1! n3 = 2 % 2; n4 = 4 % 2; REMAINDER, the MOD, for all these EVEN numbers is 0. n5 = 6 % 2; n5 == 0 is TRUE. n5 == 1 is FALSE n2 = 5 % 2; n2, an area of RAM for storing int data. --- n2 == 0 is FALSE. n2 == 1 is TRUE.There are some comments about ignoring the function evenOrOdd(n1:int):String { ... } below here, if you are interested.
Do NOT worry about this evenOrOdd function! It returns either the String "Even" or the String "Odd as its result. It is not void! However, as you can see, it has something you have never seen between the ()s.
------ ------ function evenOrOdd( n1:int ) : String { ------ ------ INPUT OUTPUT or result returned ------ ------ someFineButton_btn.addEventListener(MouseEvent.CLICK, questionInTodaysClass); -------------- ---- function questionInTodaysClass( evt:MouseEvent ) : void { -------------- ---- INPUT NO to function OUTPUT - thus VOID void, i.e. returns nothing, the function returns NOTHING at all.
roll_btn.addEventListener(MouseEvent.CLICK, roll); function roll(evt:MouseEvent) { var r1:int; var r2:int; r1 = Math.floor(Math.random() * 6 + 1); r2 = Math.floor(Math.random() * 6 + 1); d1.gotoAndStop(r1); d2.gotoAndStop(r2); /* Ignore the ActionScript code BELOW here. We will get to this during class on Thursday or Friday of this week. */ dice_txt.text = String(r1 + r2); dd1.val = Math.floor(Math.random() * 6 + 1); dd2.val = Math.floor(Math.random() * 6 + 1); dd1.gotoAndStop(dd1.val); dd2.gotoAndStop(dd2.val); dice2_txt.text = String(dd1.val + dd2.val); d1EO_txt.text = evenOrOdd(r1); d2EO_txt.text = evenOrOdd(r2); dEO_txt.text = evenOrOdd(r1 + r2); dd1EO_txt.text = evenOrOdd(dd1.val); dd2EO_txt.text = evenOrOdd(dd2.val); ddEO_txt.text = evenOrOdd(dd1.val + dd2.val); } // Do NOT worry about this evenOrOdd function! It returns either the // String "Even" or the String "Odd as its result. It is not void! // However, as you can see, it has something you have never seen between // the ()s. ------ ------ // function evenOrOdd( n1:int ) : String // ------ ------ // INPUT OUTPUT or result returned // ------ ------ function evenOrOdd(n1:int):String { if (n1 % 2 == 0) { return "Even"; } else { return "Odd"; } }