Advertisement
aandnota

Aaron's Adjustment

Sep 23rd, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.display.*;
  4.     import flash.events.*;
  5.     import flash.text.*;
  6.     import flash.utils.Timer;
  7.     import flash.media.Sound;
  8.     import flash.media.SoundChannel;
  9.     //import flash.net.URLRequest;
  10.    
  11.    
  12.    
  13.     public class AaronsMemoryGame extends Sprite
  14.     {
  15.         static const numLights:uint = 5;  // 5 lights are used in game
  16.        
  17.         private var lights:Array; // array containing the light objects
  18.         private var playOrder:Array; // Part 1 of the sequence of lights controls
  19.         private var repeatOrder:Array; //Part 2 of the sequence of lights controls
  20.        
  21.         private var textMessage:TextField; // in game messages
  22.         private var textScore:TextField; // in game score
  23.        
  24.         private var lightTimer:Timer; // how long light has been on
  25.         private var offTimer:Timer; // how long light has been off
  26.        
  27.         var gameMode:String; // play or replay
  28.         var currentSelection:MovieClip = null;  // sets starting movie to default
  29.        
  30.         // defines the sounds used by the game
  31.         var noteOneSound:Note1 = new Note1();
  32.         var noteTwoSound:Note2 = new Note2();
  33.         var noteThreeSound:Note3 = new Note3();
  34.         var noteFourSound:Note4 = new Note4();
  35.         var noteFiveSound:Note5 = new Note5();
  36.        
  37.         public function AaronsMemoryGame()
  38.         {
  39.             // text formatting for game screen
  40.             var textFormat = new TextFormat();
  41.             textFormat.font = "Arial";
  42.             textFormat.size = 24;
  43.             textFormat.align = "center";
  44.            
  45.             // code for upper text field - paramets for message field
  46.             textMessage = new TextField();
  47.             textMessage.width = 550;
  48.             textMessage.y = 110;
  49.             textMessage.selectable = false;
  50.             textMessage.defaultTextFormat = textFormat;
  51.             addChild(textMessage);
  52.                        
  53.             // code for lower text field - parameters for score field
  54.             textScore = new TextField();
  55.             textScore.width = 550;
  56.             textScore.y = 250;
  57.             textScore.selectable = false;
  58.             textScore.defaultTextFormat = textFormat;
  59.             addChild(textScore);
  60.            
  61.             // make the lights
  62.             lights = new Array();
  63.             for(var i:uint=0;i<numLights;i++)
  64.             {
  65.                 var thisLight:Light = new Light();
  66.                 thisLight.lightColors.gotoAndStop(i+1);  // this shows the proper frame for the lights
  67.                 thisLight.x = i*75+100;  // x position of light
  68.                 thisLight.y = 175;  // y position of light
  69.                 thisLight.lightNum = i; // to remember the light number
  70.                 lights.push(thisLight); // adds addition light flashes to the array
  71.                 addChild(thisLight);  // displays it on the screen
  72.                 thisLight.addEventListener(MouseEvent.CLICK,clickLight); // lights can be clicked
  73.                 thisLight.buttonMode = true;
  74.             }
  75.            
  76.             // resets the sequence to start over, also runs at start
  77.             playOrder = new Array();
  78.             gameMode = "play";
  79.             nextTurn();
  80.        
  81.         }
  82.        
  83.         // adds to the sequence and starts gameplay up again
  84.         public function nextTurn()
  85.         {
  86.             //this code adds another light to the sequence
  87.             var r:uint = Math.floor(Math.random()*numLights);
  88.             playOrder.push(r);
  89.            
  90.             // this code displays the text
  91.             textMessage.text = "Watch and Listen";
  92.             textScore.text = "Sequence Lenght: "+playOrder.length;
  93.            
  94.             // set up timers to show sequence
  95.             lightTimer = new Timer(1000,playOrder.length+1);
  96.             lightTimer.addEventListener(TimerEvent.TIMER,lightSequence);
  97.            
  98.             // start the time
  99.             lightTimer.start();
  100.            
  101.         }
  102.        
  103.         // play next light in sequence
  104.         public function lightSequence(event:TimerEvent)
  105.         {
  106.             // current location in sequence
  107.             var playStep:uint = event.currentTarget.currentCount-1;
  108.             if(playStep < playOrder.length) // sequence continuing
  109.             {
  110.                 lightOn(playOrder[playStep]);
  111.             }
  112.             else //sequence ends
  113.             {
  114.                 startPlayerRepeat();
  115.             }
  116.         }
  117.        
  118.         // starts the repeation code
  119.         public function startPlayerRepeat()
  120.         {
  121.             currentSelection = null;
  122.             textMessage.text = "Repeat.";
  123.             gameMode = "replay";
  124.             repeatOrder = playOrder.concat();
  125.         }
  126.        
  127.          // controls turning on the light and how long it stays on
  128.         public function lightOn(newLight)
  129.         {
  130.             // plays appropriate sounds
  131.             if (newLight == 1)
  132.             {
  133.                 playSound(noteOneSound);
  134.             }
  135.             else if (newLight == 2)
  136.             {
  137.                 playSound(noteTwoSound);
  138.             }
  139.             else if (newLight == 3)
  140.             {
  141.                 playSound(noteThreeSound);
  142.             }
  143.             else if (newLight == 4)
  144.             {
  145.                 playSound(noteFourSound);
  146.             }
  147.             else if (newLight == 5)
  148.             {
  149.                 playSound(noteFiveSound);
  150.             }          
  151.             currentSelection = lights[newLight];
  152.             currentSelection.gotoAndStop(2);  // turns the light on
  153.             offTimer = new Timer(500,1); // timer for turning light off
  154.             offTimer.addEventListener(TimerEvent.TIMER_COMPLETE,lightOff);
  155.             offTimer.start();
  156.         }
  157.          
  158.          // this function turns the light off if it is still on
  159.         public function lightOff(event:TimerEvent)
  160.         {
  161.             if(currentSelection != null)
  162.             {
  163.                 currentSelection.gotoAndStop(1);
  164.                 currentSelection = null;
  165.                 offTimer.stop();
  166.             }
  167.         }
  168.        
  169.         // receive the mouse clicks on the lights
  170.         public function clickLight(event:MouseEvent)
  171.         {
  172.             // prevent mouse clicks while sequence is displaying
  173.             if(gameMode != "replay") return;
  174.            
  175.             // turn off light if it didnt shut off
  176.             lightOff(null);
  177.            
  178.             // when there is a match
  179.             if(event.currentTarget.lightNum == repeatOrder.shift())
  180.             {
  181.                 lightOn(event.currentTarget.lightNum);
  182.                 // checks if sequence is done
  183.                 if(repeatOrder.length == 0)
  184.                 {
  185.                     nextTurn();
  186.                 }
  187.             }
  188.             else // didn't get it
  189.             {
  190.                     textMessage.text = "Game Over!";
  191.                     gameMode = "gameover";
  192.             }
  193.         }
  194.         public function playSound(soundObject:Object)
  195.         {
  196.             var channel:SoundChannel = soundObject.play();
  197.         }
  198.  
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement