Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- stage.align = "TL";
- //makes the flash content align to the top left of the screen
- stage.scaleMode = "noScale";
- //disables scaling of content when you resize the window
- var colors:Array = [ 0x0000FF, 0xFF0000, 0xFFFF00, 0x00FF00, 0xFF00FF, 0xAAAAAA, 0xFF9999, 0x3399FF ];
- //[blue, red, yellow, green, purple, gray, pink, teal]
- var pieces:Array = [];
- var lastPieceClicked:Object;
- var currentPieceClicked:Object;
- var needToReset:Boolean = false;
- var timer:Timer = new Timer(1000, 1);
- timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerEnded);
- function timerEnded(evt:TimerEvent):void{
- clearPieces();
- }
- initializeGame();
- function initializeGame():void{
- colors = colors.concat(colors);
- //combines the color array with a copy of itself
- //[ 0x0000FF, 0xFF0000, 0xFFFF00, 0x00FF00, 0xFF00FF, 0xAAAAAA, 0xFF9999, 0x3399FF, 0x0000FF, 0xFF0000, 0xFFFF00, 0x00FF00, 0xFF00FF, 0xAAAAAA, 0xFF9999, 0x3399FF ]
- var colorsLength:int = colors.length;
- for(var i:int = 0; i < colorsLength; i++){
- pieces[i] = new MovieClip();
- //create a movieclip (it's just a display object)
- pieces[i].graphics.beginFill(0x000000, 1);
- pieces[i].graphics.drawRect(0,0,100,100);
- //draw a black square 100x100 pixels big
- pieces[i].addEventListener(MouseEvent.CLICK, pieceClicked);
- //add an event listener, this one listens for when the mouse clicks on the display object
- //once the object is clicked it calls the pieceClicked method.
- pieces[i].x = i % 4 * 120 + 10;
- pieces[i].y = Math.floor( i / 4) * 120 + 10;
- //position the piece on the stage
- var colorIndex:int = Math.floor( Math.random() * colors.length );
- pieces[i].color = colors[colorIndex];
- //sets the color property of the piece. We'll use that later.
- colors.splice(colorIndex, 1);
- //remove the color from the colors array so we don't reuse it
- //there are 2 copies of each color so we will have 2 pieces of each color
- addChild(pieces[i]);
- //adds the piece to the stage so we can actually see it.
- }
- }
- function pieceClicked(evt:MouseEvent):void{
- if(needToReset){
- timer.stop();
- clearPieces();
- //if the pieces haven't been reset yet by the timer before the player clicks on another piece, it stops the timer
- //and resets the board before showing the card that was clicked
- }
- if(lastPieceClicked && evt.currentTarget != lastPieceClicked){
- //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
- //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
- currentPieceClicked = evt.currentTarget;
- //set the second piece that was clicked
- needToReset = true;
- //needToReset is used at the beginning of the function to see if the player click on the pieces before the timer reset the board
- timer.reset();
- //reset the timer to the full length
- timer.start();
- //start the timer
- }else{
- //this part of the conditional is run if a first piece hasn't been clicked already
- //it just sets lastPieceClicked variable
- lastPieceClicked = evt.currentTarget;
- }
- showPiece(evt.currentTarget);
- }
- function showPiece(obj:Object):void{
- obj.graphics.clear();
- obj.graphics.beginFill(obj.color, 1);
- obj.graphics.drawRect(0,0,100,100);
- }
- function clearPieces():void{
- //compares pieces
- if(lastPieceClicked.color == currentPieceClicked.color){
- //if the last piece and the current piece have the same color...
- removeChild(MovieClip(lastPieceClicked));
- removeChild(MovieClip(currentPieceClicked));
- //remove the movieclips so that they aren't in play anymore.
- // MovieClip( *** ) casts the object inside the paranthesis as a MovieClip
- // this is done because flash does not know that those objects are movieclips and removeChild expects a movieclip
- // you can ignore "casting" for now, i'll go into greater depth later
- }else{
- // WRONG CHOICE
- }
- needToReset = false;
- lastPieceClicked = null;
- //set lastPieceClicked to null so there is no "last piece"
- for(var i:int = 0; i < pieces.length; i++){
- pieces[i].graphics.clear();
- pieces[i].graphics.beginFill(0x000000, 1);
- pieces[i].graphics.drawRect(0,0,100,100);
- //resets each piece so that it's black
- }
- }
Add Comment
Please, Sign In to add comment