Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import flash.display.*;
- import flash.events.*;
- import flash.text.*;
- import flash.utils.Timer;
- import flash.media.Sound;
- import flash.media.SoundChannel;
- //import flash.net.URLRequest;
- public class AaronsMemoryGame extends Sprite
- {
- static const numLights:uint = 5; // 5 lights are used in game
- private var lights:Array; // array containing the light objects
- private var playOrder:Array; // Part 1 of the sequence of lights controls
- private var repeatOrder:Array; //Part 2 of the sequence of lights controls
- private var textMessage:TextField; // in game messages
- private var textScore:TextField; // in game score
- private var lightTimer:Timer; // how long light has been on
- private var offTimer:Timer; // how long light has been off
- var gameMode:String; // play or replay
- var currentSelection:MovieClip = null; // sets starting movie to default
- // defines the sounds used by the game
- var noteOneSound:Note1 = new Note1();
- var noteTwoSound:Note2 = new Note2();
- var noteThreeSound:Note3 = new Note3();
- var noteFourSound:Note4 = new Note4();
- var noteFiveSound:Note5 = new Note5();
- public function AaronsMemoryGame()
- {
- // text formatting for game screen
- var textFormat = new TextFormat();
- textFormat.font = "Arial";
- textFormat.size = 24;
- textFormat.align = "center";
- // code for upper text field - paramets for message field
- textMessage = new TextField();
- textMessage.width = 550;
- textMessage.y = 110;
- textMessage.selectable = false;
- textMessage.defaultTextFormat = textFormat;
- addChild(textMessage);
- // code for lower text field - parameters for score field
- textScore = new TextField();
- textScore.width = 550;
- textScore.y = 250;
- textScore.selectable = false;
- textScore.defaultTextFormat = textFormat;
- addChild(textScore);
- // make the lights
- lights = new Array();
- for(var i:uint=0;i<numLights;i++)
- {
- var thisLight:Light = new Light();
- thisLight.lightColors.gotoAndStop(i+1); // this shows the proper frame for the lights
- thisLight.x = i*75+100; // x position of light
- thisLight.y = 175; // y position of light
- thisLight.lightNum = i; // to remember the light number
- lights.push(thisLight); // adds addition light flashes to the array
- addChild(thisLight); // displays it on the screen
- thisLight.addEventListener(MouseEvent.CLICK,clickLight); // lights can be clicked
- thisLight.buttonMode = true;
- }
- // resets the sequence to start over, also runs at start
- playOrder = new Array();
- gameMode = "play";
- nextTurn();
- }
- // adds to the sequence and starts gameplay up again
- public function nextTurn()
- {
- //this code adds another light to the sequence
- var r:uint = Math.floor(Math.random()*numLights);
- playOrder.push(r);
- // this code displays the text
- textMessage.text = "Watch and Listen";
- textScore.text = "Sequence Lenght: "+playOrder.length;
- // set up timers to show sequence
- lightTimer = new Timer(1000,playOrder.length+1);
- lightTimer.addEventListener(TimerEvent.TIMER,lightSequence);
- // start the time
- lightTimer.start();
- }
- // play next light in sequence
- public function lightSequence(event:TimerEvent)
- {
- // current location in sequence
- var playStep:uint = event.currentTarget.currentCount-1;
- if(playStep < playOrder.length) // sequence continuing
- {
- lightOn(playOrder[playStep]);
- }
- else //sequence ends
- {
- startPlayerRepeat();
- }
- }
- // starts the repeation code
- public function startPlayerRepeat()
- {
- currentSelection = null;
- textMessage.text = "Repeat.";
- gameMode = "replay";
- repeatOrder = playOrder.concat();
- }
- // controls turning on the light and how long it stays on
- public function lightOn(newLight)
- {
- // plays appropriate sounds
- if (newLight == 1)
- {
- playSound(noteOneSound);
- }
- else if (newLight == 2)
- {
- playSound(noteTwoSound);
- }
- else if (newLight == 3)
- {
- playSound(noteThreeSound);
- }
- else if (newLight == 4)
- {
- playSound(noteFourSound);
- }
- else if (newLight == 5)
- {
- playSound(noteFiveSound);
- }
- currentSelection = lights[newLight];
- currentSelection.gotoAndStop(2); // turns the light on
- offTimer = new Timer(500,1); // timer for turning light off
- offTimer.addEventListener(TimerEvent.TIMER_COMPLETE,lightOff);
- offTimer.start();
- }
- // this function turns the light off if it is still on
- public function lightOff(event:TimerEvent)
- {
- if(currentSelection != null)
- {
- currentSelection.gotoAndStop(1);
- currentSelection = null;
- offTimer.stop();
- }
- }
- // receive the mouse clicks on the lights
- public function clickLight(event:MouseEvent)
- {
- // prevent mouse clicks while sequence is displaying
- if(gameMode != "replay") return;
- // turn off light if it didnt shut off
- lightOff(null);
- // when there is a match
- if(event.currentTarget.lightNum == repeatOrder.shift())
- {
- lightOn(event.currentTarget.lightNum);
- // checks if sequence is done
- if(repeatOrder.length == 0)
- {
- nextTurn();
- }
- }
- else // didn't get it
- {
- textMessage.text = "Game Over!";
- gameMode = "gameover";
- }
- }
- public function playSound(soundObject:Object)
- {
- var channel:SoundChannel = soundObject.play();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement