Guest User

Untitled

a guest
Oct 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. stage.align = "TL";
  2. //makes the flash content align to the top left of the screen
  3. stage.scaleMode = "noScale";
  4. //disables scaling of content when you resize the window
  5.  
  6. var colors:Array = [ 0x0000FF, 0xFF0000, 0xFFFF00, 0x00FF00, 0xFF00FF, 0xAAAAAA, 0xFF9999, 0x3399FF ];
  7. //[blue, red, yellow, green, purple, gray, pink, teal]
  8.  
  9. var pieces:Array = [];
  10. var lastPieceClicked:Object;
  11. var currentPieceClicked:Object;
  12. var needToReset:Boolean = false;
  13.  
  14.  
  15. var timer:Timer = new Timer(1000, 1);
  16. timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerEnded);
  17. function timerEnded(evt:TimerEvent):void{
  18.     clearPieces();
  19. }
  20.  
  21.  
  22. initializeGame();
  23. function initializeGame():void{
  24.     colors = colors.concat(colors);
  25.     //combines the color array with a copy of itself
  26.     //[ 0x0000FF, 0xFF0000, 0xFFFF00, 0x00FF00, 0xFF00FF, 0xAAAAAA, 0xFF9999, 0x3399FF, 0x0000FF, 0xFF0000, 0xFFFF00, 0x00FF00, 0xFF00FF, 0xAAAAAA, 0xFF9999, 0x3399FF ]
  27.    
  28.     var colorsLength:int = colors.length;
  29.     for(var i:int = 0; i < colorsLength; i++){
  30.         pieces[i] = new MovieClip();
  31.         //create a movieclip (it's just a display object)
  32.        
  33.        
  34.         pieces[i].graphics.beginFill(0x000000, 1);
  35.         pieces[i].graphics.drawRect(0,0,100,100);
  36.         //draw a black square 100x100 pixels big
  37.        
  38.        
  39.         pieces[i].addEventListener(MouseEvent.CLICK, pieceClicked);
  40.         //add an event listener, this one listens for when the mouse clicks on the display object
  41.         //once the object is clicked it calls the pieceClicked method.
  42.        
  43.         pieces[i].x = i % 4 * 120 + 10;
  44.         pieces[i].y = Math.floor( i / 4) * 120 + 10;
  45.         //position the piece on the stage
  46.        
  47.        
  48.         var colorIndex:int = Math.floor( Math.random() * colors.length );
  49.         pieces[i].color = colors[colorIndex];
  50.         //sets the color property of the piece. We'll use that later.
  51.        
  52.         colors.splice(colorIndex, 1);
  53.         //remove the color from the colors array so we don't reuse it
  54.         //there are 2 copies of each color so we will have 2 pieces of each color
  55.        
  56.         addChild(pieces[i]);
  57.         //adds the piece to the stage so we can actually see it.
  58.        
  59.     }
  60. }
  61. function pieceClicked(evt:MouseEvent):void{
  62.     if(needToReset){
  63.         timer.stop();
  64.         clearPieces();
  65.         //if the pieces haven't been reset yet by the timer before the player clicks on another piece, it stops the timer
  66.         //and resets the board before showing the card that was clicked    
  67.     }
  68.    
  69.     if(lastPieceClicked && evt.currentTarget != lastPieceClicked){
  70.         //this checks to see if lastPieceClicked is null or not and it also checks to make sure the second piece is not the same as the first
  71.         //if it's not null (as in the variable was set, that means 2 pieces have been clicked already. the last pieces and the current piece
  72.        
  73.        
  74.         currentPieceClicked = evt.currentTarget;
  75.         //set the second piece that was clicked
  76.        
  77.         needToReset = true;
  78.         //needToReset is used at the beginning of the function to see if the player click on the pieces before the timer reset the board
  79.         timer.reset();
  80.         //reset the timer to the full length
  81.         timer.start();
  82.         //start the timer      
  83.     }else{
  84.         //this part of the conditional is run if a first piece hasn't been clicked already
  85.         //it just sets lastPieceClicked variable
  86.         lastPieceClicked = evt.currentTarget;
  87.        
  88.     }
  89.    
  90.     showPiece(evt.currentTarget);
  91.    
  92. }
  93. function showPiece(obj:Object):void{
  94.     obj.graphics.clear();
  95.     obj.graphics.beginFill(obj.color, 1);
  96.     obj.graphics.drawRect(0,0,100,100);
  97. }
  98. function clearPieces():void{
  99.     //compares pieces
  100.     if(lastPieceClicked.color == currentPieceClicked.color){
  101.             //if the last piece and the current piece have the same color...
  102.            
  103.         removeChild(MovieClip(lastPieceClicked));
  104.         removeChild(MovieClip(currentPieceClicked));           
  105.         //remove the movieclips so that they aren't in play anymore.
  106.         // MovieClip( *** ) casts the object inside the paranthesis as a MovieClip
  107.         // this is done because flash does not know that those objects are movieclips and removeChild expects a movieclip
  108.         // you can ignore "casting" for now, i'll go into greater depth later
  109.     }else{
  110.         // WRONG CHOICE
  111.        
  112.     }
  113.        
  114.    
  115.     needToReset = false;
  116.     lastPieceClicked = null;
  117.     //set lastPieceClicked to null so there is no "last piece"
  118.     for(var i:int = 0; i < pieces.length; i++){    
  119.         pieces[i].graphics.clear();
  120.         pieces[i].graphics.beginFill(0x000000, 1);
  121.         pieces[i].graphics.drawRect(0,0,100,100);
  122.         //resets each piece so that it's black     
  123.     }
  124. }
Add Comment
Please, Sign In to add comment