Advertisement
aandnota

Untitled

Sep 29th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.     import flash.display.*;
  3.     import flash.events.*;
  4.     import flash.text.*;
  5.     import flash.utils.Timer;
  6.     import flash.media.Sound;
  7.     import flash.media.SoundChannel;
  8.     import flash.net.URLRequest;
  9.    
  10.     public class MemoryGame extends Sprite {
  11.         static const numLights:uint = 5;
  12.            
  13.         private var lights:Array; // list of light objects
  14.         private var playOrder:Array; // growing sequence
  15.         private var repeatOrder:Array;
  16.        
  17.         // text message
  18.         private var textMessage:TextField;
  19.         private var textScore:TextField;
  20.        
  21.         // timers
  22.         private var lightTimer:Timer;
  23.         private var offTimer:Timer;
  24.        
  25.         var gameMode:String; // play or replay
  26.         var currentSelection:MovieClip = null;
  27.        
  28.         var noteOneSound:Note1 = new Note1();
  29.         var noteTwoSound:Note2 = new Note2();
  30.         var noteThreeSound:Note3 = new Note3();
  31.         var noteFourSound:Note4 = new Note4();
  32.         var noteFiveSound:Note5 = new Note5();
  33.        
  34.         public function MemoryGame() {
  35.             // text formating
  36.             var textFormat = new TextFormat();
  37.             textFormat.font = "Arial";
  38.             textFormat.size = 24;
  39.             textFormat.align = "center";
  40.            
  41.             // create the upper text field
  42.             textMessage = new TextField();
  43.             textMessage.width = 550;
  44.             textMessage.y = 110;
  45.             textMessage.selectable = false;
  46.             textMessage.defaultTextFormat = textFormat;
  47.             addChild(textMessage);
  48.            
  49.             // create the lower text field
  50.             textScore = new TextField();
  51.             textScore.width = 550;
  52.             textScore.y = 250;
  53.             textMessage.selectable = false;
  54.             textScore.defaultTextFormat = textFormat;
  55.             addChild(textScore);
  56.            
  57.                            
  58.             // make lights 
  59.             lights = new Array();
  60.             for(var i:uint=0;i<numLights;i++) {
  61.                 var thisLight:Light = new Light();
  62.                 thisLight.lightColors.gotoAndStop(i+1); //show proper frame
  63.                 thisLight.x = i*75+100; // position
  64.                 thisLight.y = 175;
  65.                 thisLight.lightNum = i; // remember light number
  66.                 lights.push(thisLight); // add to array of lights
  67.                 addChild(thisLight); // add to array of lights
  68.                 thisLight.addEventListener(MouseEvent.CLICK,clickLight);
  69.                 thisLight.buttonMode = true;
  70.             }
  71.            
  72.             // reset sequence, do first turn
  73.             playOrder = new Array();
  74.             gameMode = "play";
  75.             nextTurn();
  76.         }
  77.        
  78.         // add one to the sequence and start
  79.         public function nextTurn() {
  80.             // add new light to sequence
  81.             var r:uint = Math.floor(Math.random()*numLights);
  82.             playOrder.push(r);
  83.            
  84.             // show text
  85.             textMessage.text = "Watch and Listen.";
  86.             textScore.text = "Sequence Length: "+playOrder.length;
  87.            
  88.             // set up timers to show sequence
  89.             lightTimer = new Timer(1000,playOrder.length+1);
  90.             lightTimer.addEventListener(TimerEvent.TIMER,lightSequence);
  91.            
  92.             // start timer
  93.             lightTimer.start();
  94.         }
  95.        
  96.         // play next in sequence
  97.         public function lightSequence(event:TimerEvent) {
  98.             // where are we in the sequence
  99.             var playStep:uint = event.currentTarget.currentCount-1;
  100.            
  101.             if (playStep < playOrder.length) { // not last time
  102.                 lightOn(playOrder[playStep]);
  103.             } else { // sequence over
  104.                 startPlayerRepeat();
  105.             }
  106.         }
  107.        
  108.         // start player repetition
  109.         public function startPlayerRepeat() {
  110.             currentSelection = null;
  111.             textMessage.text = "Repeat.";
  112.             gameMode = "replay";
  113.             repeatOrder = playOrder.concat();
  114.         }
  115.        
  116.         // turn on light and set timer to turn it off
  117.         public function lightOn(newLight) {
  118.             if (newLight == 1) {
  119.                 playSound(noteOneSound);
  120.             } else if (newLight == 2) {
  121.                 playSound (noteTwoSound);
  122.             } else if (newLight == 3) {
  123.                 playSound(noteThreeSound);
  124.             } else if (newLight == 4) {
  125.                 playSound (noteFourSound);
  126.             } else if (newLight == 5) {
  127.                 playSound(noteFiveSound);
  128.             }
  129.             currentSelection = lights[newLight];
  130.             currentSelection.gotoAndStop(2); // turn on light
  131.             offTimer = new Timer(500,1); // remember to turn it off
  132.             offTimer.addEventListener(TimerEvent.TIMER_COMPLETE,lightOff);
  133.             offTimer.start();
  134.         }
  135.        
  136.         // turn off light if it is still on
  137.         public function lightOff(event:TimerEvent) {
  138.             if (currentSelection != null) {
  139.                 currentSelection.gotoAndStop(1);
  140.                 currentSelection = null;
  141.                 offTimer.stop();
  142.             }
  143.         }
  144.        
  145.         // recieve mouse clicks on light
  146.         public function clickLight(event:MouseEvent) {
  147.             // prevent mouse clicks while showing sequence
  148.             if (gameMode != "replay") return;
  149.            
  150.             // turn off light if it hasn't gone off by itself
  151.             lightOff(null);
  152.            
  153.             // correct match
  154.             if (event.currentTarget.lightNum == repeatOrder.shift()){
  155.                 lightOn(event.currentTarget.lightNum);
  156.                
  157.                 // check to see if sequence is over
  158.                 if (repeatOrder.length == 0) {
  159.                     nextTurn();
  160.                 }
  161.                                
  162.             // got it wrong
  163.             } else {
  164.                 textMessage.text = "Game Over!";
  165.                 gameMode = "gameover";
  166.             }
  167.         }
  168.         public function playSound(soundObject:Object) {
  169.             var channel:SoundChannel = soundObject.play();
  170.         }
  171.     }
  172. }
  173.  
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement