package
{
//fash packages to import import flash.display.MovieClip import flash.events.KeyboardEvent; import flash.events.Event; import flash.text.Font; import flash.text.TextField; import flash.events.MouseEvent; import flash.ui.Keyboard; import flash.geom.Point; import flash.media.Video; import flash.media.SoundMixer; //import all flash .net packages import flash.net.*; //costum classes to import import EyeBullet; import EnemyFlower; public class GameMain extends MovieClip { //main player variable public var _player:Player; //player movement variables public const GRAVITY:Number = 2; public const BOUNCE_FACTOR:Number = 0.8; public const HIT_FORCE:Number = 20; private var _vx:Number; private var _vy:Number; //boundries and spawn point variables public var _pSpawnPoint:PSpawnPoint; public var spawn:Boolean; public var _boundries:Boundries; public var boundArray:Array = []; public var curBound:Array = []; //sky movement variables public var _starBoundries:StarBoundries; public var _cloudsSky:CloudsSky; public var _starSky:StarSky; public var skyArray:Array = []; public var curSky:Array = []; public var starSkyGo:Boolean; public var forward:Boolean; public var moveCnt:int; //player health variables public var _healthBar:HealthBar; public var _barColor:BarColor; public var maxHP:int; public var currentHP:int; public var percentHP:Number; //collision variales public var _eastCloudsCollision:CloudsCollision; public var _westCloudsCollision:CloudsCollision; public var _northCloudsCollision:CloudsCollision; public var _southCloudsCollision:CloudsCollision; //scoring variables public var score:Boolean; public var left:Boolean; public var bounceCnt:int; public var sharedObject:SharedObject; public var sScore:Number; public var _bounces:TextField; public var _highScore:TextField; public var _save:TextField; //level changing variables public var _keyDoor:KeyDoor; public var _key:Key; public var keyCollected:Boolean; public var doorOpen:Boolean; public var downPressed: Boolean; public var currentLevel:int; //animation state variables public var animationState: String = "idleAnim"; public var _playerRunAnim:PlayerRunAnim; public var _playerJumpUpAnim:PlayerJumpUpAnim; //enemy variables public var _eyeGunJump:PlayerEyeGun; public var eyeBulletList:Array = []; public var eyeBulletR:EyeBullet; public var eyeBulletJ:EyeBullet; public var eyeBulletI:EyeBullet; var enemyFlowerList:Array = []; public var _ef1:EnemyFlower; public var _ef2:EnemyFlower; public var _ef3:EnemyFlower; public var _ef4:EnemyFlower; public var _ef5:EnemyFlower; public var _ef6:EnemyFlower; public var _ef7:EnemyFlower; public var _ef8:EnemyFlower; public var _ef9:EnemyFlower; //music player variables public var _previousBtn:PlayBtn; public var _nextBtn:PlayBtn; public var _pauseBtn:PauseBtn; public var _musicTracks:MusicTracks; public var musicState: String = "workingMansBlues"; public var musicTracksList:Array = []; public var musicCnt:int = 0; public var pause:Boolean = false; public var _musicStateTxt:TextField; public var _musicStateTxt2:TextField; public function GameMain():void { //load the enemyFlowerList with these items enemyFlowerList[0]=_ef1; enemyFlowerList[1]=_ef2; enemyFlowerList[2]=_ef3; enemyFlowerList[3]=_ef4; enemyFlowerList[4]=_ef5; enemyFlowerList[5]=_ef6; enemyFlowerList[6]=_ef7; enemyFlowerList[7]=_ef8; enemyFlowerList[8]=_ef9; //load the musicTrackList with these items musicTracksList[0]="workingMansBlues"; musicTracksList[1]="mabelsDream"; musicTracksList[2]="everyTub";
sharedObject = SharedObject.getLocal("test"); // give the save data a location sScore = Number(_highScore.text); //start with 0 score //update boundries every time class is called boundArray[0] = _boundries; boundArray[1] = _starBoundries; for (var boundCnt:int; boundCnt < boundArray.length; boundCnt++) { if (boundArray[boundCnt] != null) { curBound[0] = boundArray[boundCnt]; } } //update sky every time class is called skyArray[0] = _cloudsSky; skyArray[1] = _starSky; for (var skyCnt:int; skyCnt < skyArray.length; skyCnt++) { if (skyArray[skyCnt] != null) { curSky[0] = skyArray[skyCnt]; } } updateLevel(currentLevel); //set focus for keyboard input stage.focus = stage; //add event listener for updating the screen this.addEventListener(Event.ENTER_FRAME, enterFrameHandler); //add event listener for updating key presses stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler); //add event listener for updating mouse clicks for the specified buttons _tryAgainBtn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_2); _previousBtn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame); _nextBtn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame_3); _pauseBtn.addEventListener(MouseEvent.CLICK, fl_ClickToPauseVideo); _doanloadBtn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage); //make sure that these conditions are set thus every time this class is called bounceCnt = 0; _bounces.text = String(bounceCnt); currentLevel = 1; _player.visible = false; _player.buttonMode = false; _itemBox._HUDkey.visible = false; left = false; score = true; spawn = true; if (currentLevel == 3) { spawn = false; curBound[0].visible = false; } //set the high score to the save score every time this class is called _highScore.text = String(sScore); } private function enterFrameHandler(e:Event):void { _musicStateTxt.selectable = false _musicStateTxt2.selectable = false //logic to change the music track of the music player if(musicCnt == 0) { _musicStateTxt.visible = true; _musicStateTxt2.visible = false; _musicStateTxt.text = "Working Man's Blues"; } if(musicCnt == 1) { _musicStateTxt.visible = true; _musicStateTxt2.visible = false; _musicStateTxt.text = "Mabel's Dream"; } if(musicCnt == 2) { _musicStateTxt.visible = false; _musicStateTxt2.visible = true; _musicStateTxt2.text = "Every Tub"; } //hides the player and bouondries if spawn has not occurred if(!spawn) { _player.visible = false; curBound[0].visible = false; } _highScore.text = String(sScore); //tests to see if an enemy hit us or we hit an enemy an updates accordingly EnemyHit ();
if(_player.currentLabel != animationState) { _player.gotoAndStop(animationState); } //logic to stop the music player if(!pause && _musicTracks.currentLabel != musicState) { SoundMixer.stopAll(); _musicTracks.gotoAndStop(musicState); } else if (pause) { _musicStateTxt.visible = false; _musicStateTxt2.visible = true; _musicStateTxt2.text = "Stopped"; _musicStateTxt.wordWrap = true; SoundMixer.stopAll(); trace("stop"); } //updated to switch between boundries on a given level boundArray[0] = _boundries; boundArray[1] = _starBoundries; for (var boundCnt:int; boundCnt < boundArray.length; boundCnt++) { if (boundArray[boundCnt] != null) { curBound[0] = boundArray[boundCnt]; } } //updated to switch between skies on a given level skyArray[0] = _cloudsSky; skyArray[1] = _starSky; for (var skyCnt:int; skyCnt < skyArray.length; skyCnt++) { if (skyArray[skyCnt] != null) { curSky[0] = skyArray[skyCnt]; } } //gravitate the player that is being updated _vy += GRAVITY; //move the player that is being updated _player.x += _vx; _player.y += _vy; //sky move logic that is being updated moveCnt ++; if (moveCnt == 1000 && forward) { moveCnt = 0; forward = false; trace(forward); } if (moveCnt == 1000 && !forward) { moveCnt = 0; forward = true; trace(forward); } //move the given sckys on incrimental paths along the x axis moveSky(); //process collisions that are being updated processCollisions(); //scroll the stage and keep player in the middle of screen scrollStage(); if(sharedObject.data.savedScore == null) //null check { sharedObject.data.savedScore = sScore; // set the savedScore to 0 } else { loadData(); // load the data } updateScoreText(); //update the high score text field keyHandler (); } private function keyDownHandler(e:KeyboardEvent):void { switch (e.keyCode) { case 37: //left arrow moves player left if (left) { _vx = -7; _player.scaleX = -1; } left = true; break; case 38: //up arrow moves player up and forward faster, //turns on rocket pack animation animationState = "jumpUpAnim" _vy = -20; if(left) { _vx = -10; } else if (!left) { _vx = 10; } break; case 39: //right arrow moves player right if(!left) { _vx = 7; _player.scaleX = 1; } left = false; break; case 32: //space bar fires bullets fireBullet(); //these conditions will turn on the eye bullet animations if the player's animation //state matches the given condition if (animationState == "jumpUpAnim") { MovieClip(_player._playerJumpUpAnim.getChildByName("_eyeGunJump")) .gotoAndPlay(2); } else if (animationState == "runAnim" ) { MovieClip(_player._playerRunAnim.getChildByName("_eyeGunRun")) .gotoAndPlay(2); } else if (animationState == "idleAnim" ) { MovieClip(_player._playerIdleAnim.getChildByName("_eyeGunIdle")) .gotoAndPlay(2); } default: } if(e.keyCode == Keyboard.DOWN) { downPressed = true; if(doorOpen && _player.hitTestObject(curBound[0]._keyDoor)) { //proceed to the next level if the player is touching an open door nextLevel(); } } } private function keyUpHandler(e:KeyboardEvent):void { switch (e.keyCode) { //these cases will turn the x velocity of if the corresponding buttons are not //being pushed case 37: case 39: _vx = 0; break; default: } } private function processCollisions():void { if(spawn) { if (_vy > 0) { //respawn if player falls of the stage if (spawn && _player.y > stage.stageHeight) { _player.x = _pSpawnPoint.x; _player.y = _pSpawnPoint.y; curBound[0].x = 0; curBound[0].y = 0; _vy = 0; } //otherwise, process collisions with boundries else { var collision:Boolean = false; if (curBound[0].hitTestPoint(_player.x, _player.y, true)) { collision = true; } if (collision) { _player.visible = true; while(collision) { //this command keeps the player above the collision //objects _player.y -= 1; //these condition switch the animation state of the //avitar depending on the avitars x an y velocity as //recorded from the players inputs if(_vx > 4) { animationState = "runAnim"; } else if(_vx > .1) { animationState = "walkAnim"; } else if(_vx < -4) { animationState = "runAnim"; } else if(_vx < -.1) { animationState = "walkAnim"; } else { animationState = "idleAnim"; } collision = false; //this will tell the code if the player's avitar is //colliding with the collision boundry objects if (curBound[0].hitTestPoint(_player.x, _player.y, true)) { collision = true; } } //set the up velocity back to 0, so the avitar does not fly off into space _vy = 0; } } } } } private function scrollStage():void { if (spawn) { //these commands make everything on the level follow the player when they move their //avitar curBound[0].x += (stage.stageWidth * 0.5) - _player.x; curBound[0].y += (stage.stageWidth * 0.5) - _player.y; //these commands will keep the player avitar and the spawn point in the middle of the //screen as they move _player.x = stage.stageWidth * 0.5; _player.y = stage.stageWidth * 0.5; _pSpawnPoint.x = stage.stageWidth * 0.5; } } private function updateHealthBar():void { percentHP = currentHP / maxHP; //set hp percent _healthBar._barColor.scaleX = percentHP; //use that percent to change the length of the //health bar } private function moveSky ():void { //move the clouds forward and backward on a incrimental timer if(forward) { curSky[0].x += .2; } else if(!forward) { curSky[0].x += .2; } //move the stars forward and backward on a different incrimental timer than the clouds if (forward && starSkyGo) { curSky[0].x += .1; } else if (!forward && starSkyGo) { curSky[0].x -= .1; } } function saveData():void { sharedObject.data.savedScore = sScore; // set the saved score to the current score sharedObject.flush(); // immediately save to the local drive } function loadData():void { sScore = sharedObject.data.savedScore; // set the variable sScore to the saved score so we //have something to update the highscore from level //to level, game to game } function updateScoreText():void { _save.text = String(Number(sScore)); // set the text property of the txtScore to the saved //score } function keyHandler () { if(keyCollected == false)// if we still haven't collected the key { if(_player.hitTestObject(curBound[0]._key))// if the player collides with the key { curBound[0]._key.visible = false; // hide the key from game view keyCollected = true; // set our Boolean to true score = true; _itemBox._HUDkey.visible = true; // make the HUD key appear } } if(doorOpen == false)// if the door hasn't been opened yet { if(keyCollected == true)// if the player has already collected the key { if(_player.hitTestObject(curBound[0]._keyDoor)) // check if the door and the // player are touching { UpdateHighScore(); curBound[0]._keyDoor.gotoAndStop(2); // switch the door's image to // its 2nd frame doorOpen = true; _itemBox._HUDkey.visible = false; // take the key away from the HUD // display } } } } function nextLevel():void { //this will advance the level that the player is on by incriminating the currentLevel counter //variable and //then call on the updateLevel method for the given increment condition currentLevel++; if(currentLevel == 3) { currentLevel = 1 updateLevel(currentLevel); } if(currentLevel == currentLevel) { updateLevel(currentLevel); } } function updateLevel(levelNum:int):void { trace("levelNum" + levelNum); if(levelNum == 2) { //these are level 2 loading conditions SoundMixer.stopAll(); musicCnt = 1; musicState = "mabelsDream"; _musicStateTxt.visible = true; _musicStateTxt2.visible = false; _musicStateTxt.text = "Mabel's Dream"; starSkyGo = true; _highScore.text = String(sScore); spawn = true; } else if(levelNum == 3) { //these are level 3 or retry level loading conditions SoundMixer.stopAll(); musicState = "everyTub"; _healthBar._barColor.scaleX = maxHP; musicCnt = 2; _musicStateTxt.visible = false; _musicStateTxt2.visible = true; _musicStateTxt2.text = "Every Tub"; } else //these are default level loading conditions if (sScore >= 50) { SoundMixer.stopAll(); musicCnt = 2; musicState = "everyTub"; _musicStateTxt.visible = false; _musicStateTxt2.visible = true; _musicStateTxt2.text = "Every Tub"; } gotoAndStop(levelNum); _player.x = _pSpawnPoint.x; _player.y = _pSpawnPoint.y; curBound[0].x = 0; curBound[0].y = 0; _vy = 0; _vx = 0; keyCollected = false; //resets the keyCollected variable curBound[0]._key.visible = true; //makes the key visible again doorOpen = false; //resets the doorOpen variable curBound[0]._keyDoor.gotoAndStop(1); //makes the door return to its locked image downPressed = false; currentLevel = levelNum; maxHP = 100; currentHP = maxHP; forward = true; moveCnt = 0; _bounces.text = "0"; _highScore.text = String(sScore); score = true; updateScoreText(); spawn = true; } function fl_ClickToGoToAndStopAtFrame_2(event:MouseEvent):void { updateLevel(1); //this will restart the game from the first level when the player pushes the //given retry button } function fl_ClickToGoToAndPlayFromFrame(event:MouseEvent):void { //this will start music if stopped and cycle trough to one of three tracks in the negative //direction when the corresponding previous button is pushed pause = false; SoundMixer.stopAll(); musicCnt--; if(musicCnt > 2) { musicCnt = 0; } if(musicCnt < 0) { musicCnt = 2; } trace(musicCnt); trace(musicTracksList[musicCnt]); musicState = musicTracksList[musicCnt]; }
function fl_ClickToGoToAndPlayFromFrame_3(event:MouseEvent):void { //this will start music if stopped and cycle trough to one of three tracks in the positive //direction when the corresponding next button is pushed pause = false; SoundMixer.stopAll(); musicCnt++; if(musicCnt > 2) { musicCnt = 0; } if(musicCnt < 0) { musicCnt = 2; } trace(musicCnt); trace(musicTracksList[musicCnt]); musicState = musicTracksList[musicCnt]; } function fl_ClickToPauseVideo(event:MouseEvent):void { if (!pause) { pause = true; //this will prevent music from being played trace(pause); } } function fl_ClickToGoToWebPage(event:MouseEvent):void { navigateToURL(new URLRequest ("http://jazz-on- line.com/artists/King_Oliver.htm"), "_blank"); //opens a blank web page to the given link in the first set of " " } function fireBullet():void { var playerDirection:String; if(_player.scaleX < 0) { playerDirection = "left"; //changes the way the player's bullets are facing to left } else if(_player.scaleX > 0) { playerDirection = "right"; //changes the way the player's bullets are facing to right } if(animationState == "jumpUpAnim") { eyeBulletJ = new EyeBullet(_player.x,_player.y - 140, playerDirection); //initializes the eye bullets at the proper location for jump anim stage.addChild(eyeBulletJ); eyeBulletList.push(eyeBulletJ); eyeBulletJ.addEventListener(Event.REMOVED,eyeBulletRemoved); trace(eyeBulletList); } else if (animationState == "runAnim") { eyeBulletR = new EyeBullet(_player.x,_player.y - 110, playerDirection); //initializes the eye bullets at the proper location for run anim stage.addChild(eyeBulletR); eyeBulletList.push(eyeBulletR); eyeBulletR.addEventListener(Event.REMOVED,eyeBulletRemoved); } else if (animationState == "idleAnim") { eyeBulletI = new EyeBullet(_player.x,_player.y - 110, playerDirection); //initializes the eye bullets at the proper location for idle anim stage.addChild(eyeBulletI); eyeBulletList.push(eyeBulletI); eyeBulletI.addEventListener(Event.REMOVED,eyeBulletRemoved); } } function eyeBulletRemoved(e:Event):void { e.currentTarget.removeEventListener(Event.REMOVED, eyeBulletRemoved); //this just removes the eventListener so we don't get an error eyeBulletList.splice(eyeBulletList.indexOf(e.currentTarget), 1); //this removes 1 object from the eyebulletList, //at the index of whatever object caused this function to activate } function UpdateHighScore() { if (_highScore.text != null && _bounces.text != null && score) //nul check { sScore = Number(_highScore.text) + Number(_bounces.text); //update saved score with the high score and bounce total _bounces.text = "0"; } else { sScore = 0; //if saved score is null make it zero } saveData(); //save the score } function BuncesPlus () { _bounces.text = String(Number(_bounces.text) + 1); //incriment up the bounce text num total every time this function triggers bounceCnt++; } function LowerHealth () { currentHP -= 5; if(currentHP <= 0) //if the player died { currentHP = 0; updateLevel(3); //go to try again scene SoundMixer.stopAll(); //reset the music to a new track musicState = "everyTub"; _healthBar._barColor.scaleX = maxHP; //reset the hp bar to full musicCnt = 2; _musicStateTxt.visible = false; _musicStateTxt2.visible = true; _musicStateTxt2.text = "Every Tub"; } updateHealthBar(); //update the healthBar } function EnemyHit () { if (eyeBulletList.length == 0) { if (_player.hitTestObject(curBound[0]._ef1)) { LowerHealth(); } else if (_player.hitTestObject(curBound[0]._ef2)) { LowerHealth(); } else if (_player.hitTestObject(curBound[0]._ef3)) { LowerHealth(); } else if (_player.hitTestObject(curBound[0]._ef4)) { LowerHealth(); } else if (_player.hitTestObject(curBound[0]._ef5)) { LowerHealth(); } else if (_player.hitTestObject(curBound[0]._ef6)) { LowerHealth(); } else if (_player.hitTestObject(curBound[0]._ef7)) { LowerHealth(); } else if (_player.hitTestObject(curBound[0]._ef8)) { LowerHealth(); } else if (_player.hitTestObject(curBound[0]._ef9)) { LowerHealth(); } } if (eyeBulletList.length > 0) { for (var cnt = 0; cnt < eyeBulletList.length; cnt++) { if (eyeBulletList[cnt].hitTestObject(curBound[0]._ef1)) { curBound[0]._ef1.removeSelf(); BuncesPlus(); } else if (eyeBulletList[cnt].hitTestObject(curBound[0]._ef2)) { curBound[0]._ef2.removeSelf(); BuncesPlus(); } else if (eyeBulletList[cnt].hitTestObject(curBound[0]._ef3)) { curBound[0]._ef3.removeSelf(); BuncesPlus(); } else if (eyeBulletList[cnt].hitTestObject(curBound[0]._ef4)) { curBound[0]._ef4.removeSelf(); BuncesPlus(); } else if (eyeBulletList[cnt].hitTestObject(curBound[0]._ef5)) { curBound[0]._ef5.removeSelf(); BuncesPlus(); } else if (eyeBulletList[cnt].hitTestObject(curBound[0]._ef6)) { curBound[0]._ef6.removeSelf(); BuncesPlus(); } else if (eyeBulletList[cnt].hitTestObject(curBound[0]._ef7)) { curBound[0]._ef7.removeSelf(); BuncesPlus(); } else if (eyeBulletList[cnt].hitTestObject(curBound[0]._ef8)) { curBound[0]._ef8.removeSelf(); BuncesPlus(); } else if (eyeBulletList[cnt].hitTestObject(curBound[0]._ef9)) { curBound[0]._ef9.removeSelf(); BuncesPlus(); } } } } }
}