PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/FlashIDE/project42/src/com/flashandmath/DominoPull.as

https://github.com/markmatthewsphd/flash42
ActionScript | 268 lines | 137 code | 58 blank | 73 comment | 5 complexity | 096f7d1680fb41645db5b2a06df4f8ee MD5 | raw file
  1. /*
  2. Flash CS4+ AS3 tutorial by Doug Ensley.
  3. http://www.flashandmath.com/
  4. Last modified: December 5, 2010
  5. */
  6. /*The domino images used were created by Mark W. Matthews.
  7. ********************
  8. DominoPull class
  9. *** adapted by Mark W. Matthews
  10. *********************************/
  11. package
  12. {
  13. import flash.display.Sprite;
  14. import flash.display.Bitmap;
  15. import flash.display.BitmapData;
  16. import flash.display.SimpleButton;
  17. import flash.events.*;
  18. import flash.text.*;
  19. import flash.utils.Timer;
  20. import flash.geom.PerspectiveProjection;
  21. import flash.geom.Point;
  22. import fl.transitions.Tween;
  23. import fl.transitions.TweenEvent;
  24. import fl.transitions.easing.*;
  25. import flash.geom.Vector3D;
  26. // Load the custom classes for working with dominoes
  27. import Domino;
  28. import DominoLoader;
  29. public class DominoPull
  30. {
  31. private var btn_pullButton:SimpleButton;
  32. private var btn_shakeButton:SimpleButton;
  33. private var board:Sprite;
  34. private var txtOutput:TextField;
  35. public function DominoPull():void
  36. {
  37. // set visibility for Pull and Shake buttons
  38. btn_pullButton.visible = false;
  39. btn_shakeButton.visible = false;
  40. txtOutput.wordWrap=true;
  41. txtOutput.mouseEnabled=false;
  42. txtOutput.text="Loading images. Please wait...";
  43. /*
  44. Create a board that we can attach all objects to. This will allow us to move the effect easily to different parts of the stage, if desired.
  45. var board:Sprite = new Sprite();
  46. board.x = 50;
  47. board.y = 110;
  48. addChild(board);
  49. */
  50. // Array to hold loaded dominoes and a separate array for the shuffled set:
  51. var arrDominoes:Array = [ ];
  52. var arrSet:Array = new Array(28);
  53. // Variables for things that will need to be referenced in multiple functions:
  54. var topDomino:Domino;
  55. var dominoLdr:DominoLoader;
  56. // turnRadius is radius of the semicircular path followed by y and z coordinates of each domino when pulled.
  57. var turnRadius:Number = 150;
  58. // The domino will move in "turnSteps" many steps over a "turnTime" millisecond period.
  59. var turnSteps:Number = 16;
  60. var turnTime:Number = 400;
  61. // Between dominoes pulled there is a slight pause of this many milliseconds:
  62. var delayTime:Number = 200;
  63. // A global variable counts the number of dominoes pulled so we know when to stop:
  64. var numPulled:int = 0;
  65. // Each domino will have the same perspective projection values set.
  66. var pp:PerspectiveProjection = new PerspectiveProjection();
  67. pp.fieldOfView=20;
  68. pp.projectionCenter=new Point(0,0);
  69. // The makeDominoes function sets up file names that correspond to the files in the "dominoes-50" subfolder located in the same folder as this fla source file.
  70. makeDominoes();
  71. private function makeDominoes():void {
  72. var arrSide0:Array = ["00","01","02","03","04","05","06","11","12","13","14","15","16","22","23","24","25","26","33","34","35","36","44","45","46","55","56","66"];
  73. // var arrSide1:Array = ["0","1","2","3","4","5","6"];
  74. var arrDominoStrings:Array = new Array(28);
  75. var i:int; //, j:int;
  76. // Fill arrDominoStrings with the 28 file names for the domino faces
  77. /* for (i=0; i<7; i++) {
  78. for (j=0; j<7; j++) {
  79. arrDominoStrings[7*i+j] = "dominoes-50/"+arrSide0[j]+arrSide1[i]+"-50.png";
  80. }
  81. }
  82. */
  83. for (i=0; i<28; i++) {
  84. arrDominoStrings[i] = "dominoes-50/"+arrSide0[i]+"-50.png";
  85. }
  86. /*
  87. DominoLoader object is constructed with an array of file names of domino faces
  88. and a single file name for the domino back
  89. */
  90. dominoLdr = new DominoLoader(arrDominoStrings,"dominoes-50/txback-50.png");
  91. dominoLdr.addEventListener(DominoLoader.DOMINOES_LOADED, loadComplete);
  92. dominoLdr.addEventListener(DominoLoader.LOAD_ERROR, loadError);
  93. }
  94. // Called when all domino image files have completely loaded. Primarily calls the setupPile function, defined further below.
  95. function loadComplete(e:Event):void {
  96. setupPile();
  97. btn_pullButton.visible = true;
  98. dominoLdr.removeEventListener(DominoLoader.DOMINOES_LOADED, loadComplete);
  99. dominoLdr.removeEventListener(DominoLoader.LOAD_ERROR, loadError);
  100. txtOutput.text="";
  101. txtOutput.visible=false;
  102. }
  103. // Actions to take if there is a file error:
  104. function loadError(e:Event):void {
  105. txtOutput.text="Error loading images. The server may be busy. Try refreshing the page.";
  106. dominoLdr.removeEventListener(DominoLoader.DOMINOES_LOADED, loadComplete);
  107. dominoLdr.removeEventListener(DominoLoader.LOAD_ERROR, loadError);
  108. }
  109. // Set up the pile of dominoes by "shuffling" the loaded dominoes
  110. function setupPile():void {
  111. var i:int;
  112. var r:int;
  113. var dom:Domino;
  114. // Remove all current children of board
  115. while (board.numChildren>0) {
  116. board.removeChildAt(0);
  117. }
  118. // arrSet is initially empty. arrDominoes is initially the array of
  119. // Domino objects from the DominoLoader object dominoLdr
  120. arrSet = new Array(28);
  121. arrDominoes = (dominoLdr.getDominoArray()).concat();
  122. // Remove random dominoes from arrDominoes and place them on arrSet.
  123. // Each is set to be initially facedown.
  124. for (i=0; i<28; i++) {
  125. r = Math.floor(arrDominoes.length * Math.random());
  126. dom = arrDominoes.splice(r,1)[0] as Domino;
  127. dom.makeFaceDown();
  128. dom.transform.perspectiveProjection=pp;
  129. board.addChild(dom);
  130. // Position each domino
  131. dom.x = 0;
  132. dom.y = 0;
  133. dom.z = 0;
  134. arrSet[i] = dom;
  135. }
  136. }
  137. /*
  138. Timers for managing the animation. The first timer manages the turn, and the second timer provides a delay between dominos dealt.
  139. */
  140. var tmTurn:Timer = new Timer(turnTime/turnSteps,turnSteps);
  141. tmTurn.addEventListener(TimerEvent.TIMER, tmTurn_Move);
  142. tmTurn.addEventListener(TimerEvent.TIMER_COMPLETE, tmTurn_Done);
  143. var tmDelay:Timer = new Timer(delayTime,1);
  144. tmDelay.addEventListener(TimerEvent.TIMER_COMPLETE, tmDelay_Done);
  145. // This is the main function that handles all motion on each Timer "tick."
  146. function tmTurn_Move(te:TimerEvent):void {
  147. // r is an angle (degrees) measure between 0 and 180, and ang is equivalent radian measure
  148. var r:Number = tmTurn.currentCount/turnSteps*180;
  149. var ang:Number = r*Math.PI/180;
  150. // x coordinate moves in a line, y and z move in a sine-curve arc
  151. topDomino.x = (150+35*numPulled)*r/180;
  152. topDomino.y = turnRadius*Math.sin(ang);
  153. topDomino.z = -1.5*turnRadius*Math.sin(ang);
  154. // Rotate about the horizontal axis
  155. topDomino.rotateView(180-r,"horizontal");
  156. }
  157. // When the turning timer is finished, we start the delay timer, which has no TIMER handler because its job is to fire exactly once.
  158. function tmTurn_Done(te:TimerEvent):void {
  159. /* Calling this method sets the topDomino to be truly "face up" so the
  160. domino "knows" its own orientation for possible subsequent actions.
  161. */
  162. topDomino.makeFaceUp();
  163. tmDelay.reset();
  164. tmDelay.start();
  165. }
  166. /* When the delay timer is finished (firing exactly once),
  167. we set up for the next domino to be dealt until there are five. */
  168. function tmDelay_Done(te:TimerEvent):void {
  169. if (numPulled < 7) {
  170. numPulled++;
  171. /* Remove new top domino from arrSet and ensure the topDomino is above
  172. (in the display list) all else on the board. */
  173. topDomino = arrSet.splice(arrSet.length - 1,1)[0];
  174. board.setChildIndex(topDomino,board.numChildren-1);
  175. // Start the turning timer
  176. tmTurn.reset();
  177. tmTurn.start();
  178. }
  179. else {
  180. // Show the "Shuffle" button once all 7 dominoes are pulled.
  181. btn_shakeButton.visible = true;
  182. }
  183. }
  184. // Handler for a click on the "Deal 5" button
  185. btn_pullButton.addEventListener(MouseEvent.CLICK, dealUp);
  186. function dealUp(me:MouseEvent):void {
  187. btn_pullButton.visible = false;
  188. numPulled++;
  189. /* Remove new top domino from arrSet and ensure the topDomino is above
  190. (in the display list) all else on the board. */
  191. topDomino = arrSet.splice(arrSet.length - 1,1)[0];
  192. board.setChildIndex(topDomino,board.numChildren-1);
  193. // Start the turning timer
  194. tmTurn.reset();
  195. tmTurn.start();
  196. }
  197. btn_shakeButton.addEventListener(MouseEvent.CLICK, reset);
  198. function reset(me:MouseEvent):void {
  199. numPulled = 0;
  200. setupPile();
  201. btn_shakeButton.visible = false;
  202. btn_pullButton.visible = true;
  203. }
  204. }
  205. }
  206. }