Date: Fri, 04 Apr 2008 22:11:34 -0600 (CST) From: Mark Jacobson To: 810-088-09-spring@uni.edu Subject: Friday 04/04 ActionScript and Flash... Hi Flash students, Ironically, on 04/04 we discussed the usefulness of the for statement, which seems poetically to be what the 4th day of the 4th month is especially suitable for. Here is the ActionScript 3.0 code we used in the first half of class: http://www.cs.uni.edu/~jacobson/flash/raceEm.txt And here is the ActionScript 3.0 code we transformed that into to show the usefulness of Arrays and for loops. http://www.cs.uni.edu/~jacobson/flash/raceEm2.txt ----------------------------------------------------------------- function raceEm(evt:Event):void { // Without any ARRAYS r1.x += Math.random()*12; r2.x += Math.random()*12; r3.x += Math.random()*12; r4.x += Math.random()*12; if (r1.x > finish.x || r2.x > finish.x || r3.x > finish.x || r4.x > finish.x) { tmr.stop(); } } ----------------------------------------------------------------- ------------------------------------------------------------- var r:Array = new Array(); // WITH ARRAYS and FOR loop raceEm2.txt r[0] = r1; r[1] = r2; r[2] = r3; r[3] = r4; function race(evt:Event) { for (var i:int = 0; i < 4; i++) { r[i].x += Math.random() * 21; } for (i = 0; i < 4; i++) { if (r[i].x > finish.x) { tmr.stop(); // determine the winner NOT DONE } // Need to report who won for very close race. } } ------------------------------------------------------------- On Monday, we will go over a more fully developed version of this. function race(evt:Event):void { ... ... as shown above ... for (i = 0; i < 4; i++) { if (r[i].x > finish.x) { reportWinner(); // <-- reportWinner() will be a new function... exit(); } } } function reportWinner() { tmr.stop(); Additional code to determine who came in first and somehow convey that to the user of the rectangle race software. Could have the winning rectangle start up a victory celebration animation, perhaps, for one example. } We will, of course, talk about and do some exercises and answer question related to the take home quiz that is due on Wednesday. http://www.cs.uni.edu/~jacobson/flash Mark