Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------------------------------------------------------------------
- //Program: PSP_Assignment2_Jon(s10105455k)_Shafi(s10087528k)
- //Created by: Jon Michael Jubelag & Adam Adil Shafi
- //Date Created: Week 13
- //Date Modified: 2nd Feb 2012
- //Description: The Assignment 2
- //------------------------------------------------------------------------------------------------
- package
- {
- import flash.display.*;
- import flash.utils.Dictionary;
- import flash.utils.Timer;
- import flash.events.*;
- import flash.media.Sound;
- import flash.events.MouseEvent;
- import flash.media.SoundChannel;
- import flash.ui.Mouse;
- public class GameControl extends MovieClip
- {
- //Properties
- private var platforms:Dictionary;
- private var walls:Dictionary;
- private var enemies:Dictionary;
- private var hpDrops:Dictionary;
- private var endPoints:Dictionary;
- private var item:Object;
- private var player:MovieClip;
- private var playerHealth:Number;
- private var gameTimer:Timer;
- private var playTimer:Timer;
- private var yDiff:Number;
- private var points:Number;
- private var bonusPoints:Number;
- private var suspense1:Sound;
- private var suspense2:Sound;
- private var suspense1Channel:SoundChannel;
- private var suspense2Channel:SoundChannel;
- private var musicTimer1:Timer;
- private var musicTimer2:Timer;
- private var level:MovieClip;
- private var currentLevel:Number;
- private var lastLevel:Boolean;
- private var secretHere:Boolean;
- private var hitSecret:Boolean;
- public function GameControl()
- {
- //Constructor
- gameTimer = new Timer(30, 0);
- gameTimer.addEventListener(TimerEvent.TIMER, gameLoop);
- playTimer = new Timer(1000, 120);
- playTimer.addEventListener(TimerEvent.TIMER, time);
- platforms = new Dictionary();
- walls = new Dictionary();
- enemies = new Dictionary();
- hpDrops = new Dictionary();
- endPoints = new Dictionary();
- mainMenu.playButton.addEventListener(MouseEvent.CLICK, playGame);
- mainMenu.creditsButton.addEventListener(MouseEvent.CLICK, showCredits);
- creditsMenu.backButton.addEventListener(MouseEvent.CLICK, goBack);
- endMenu.menuButton.addEventListener(MouseEvent.CLICK, toMainMenu);
- storyMenu.continueButton.addEventListener(MouseEvent.CLICK, continueTo);
- gameOverMenu.playAgainButton.addEventListener(MouseEvent.CLICK, playAgain);
- suspense1 = new music1();
- suspense2 = new music2();
- suspense1Channel = new SoundChannel();
- suspense2Channel = new SoundChannel();
- musicTimer1 = new Timer (55000, 1);
- musicTimer1.addEventListener(TimerEvent.TIMER_COMPLETE, musicTime1);
- musicTimer2 = new Timer (6000, 1);
- musicTimer2.addEventListener(TimerEvent.TIMER_COMPLETE, musicTime2);
- //Create the player
- player = new Player(this);
- currentLevel = 1;
- creditsMenu.visible = false;
- storyMenu.visible = false;
- filmGrain.visible = false;
- endMenu.visible = false;
- gameOverMenu.visible = false;
- fullHealth.visible = false;
- secretFound.visible = false;
- }
- public function initGame() //initializes the game
- {
- if(currentLevel == 1)
- {
- level = new Level1();
- level.name = "level";
- level.x = -2.7; //Spawns level with regard to displacement from stage's origin.
- level.y = -330.1;
- this.addChild(level);
- this.setChildIndex(level,1);
- lastLevel = false;
- player.x = 200;
- player.y = 100;
- points = 0;
- secretHere = true;
- hitSecret = true;
- }
- else if(currentLevel == 2)
- {
- clearDict(); //Clears all items in all dictionaries before adding them for the next level.
- level = new Level2();
- level.name = "level";
- level.x = -2.7;
- level.y = -330.1;
- this.addChild(level);
- this.setChildIndex(level,1);
- player.x = 200;
- player.y = 100;
- lastLevel = true; //Endgame content changes.
- secretHere = false;
- }
- this.yDiff = this.level.y; //Get the distance between the level and the stage's origin.
- level.visible = true;
- player.visible = true;
- for (var i:int = 0; i < this.level.numChildren; i++)
- {
- item = this.level.getChildAt(i)
- if (item is Platform)
- {
- platforms[item] = item; //Adds platform to dictionary
- }
- else if (item is Wall)
- {
- walls[item] = item;
- }
- else if (item is Enemy)
- {
- item.toMain(this);
- enemies[item] = item;
- }
- else if (item is Pickups)
- {
- hpDrops[item] = item;
- }
- else if (item is EndPoint)
- {
- endPoints[item] = item;
- }
- }
- this.addChild(player); //add player to main timeline
- this.playerHealth = 5;
- checkHealth();
- //Start Timer
- gameTimer.start();
- playTimer.start();
- bonusPoints = 120;
- suspense1.play();
- musicTimer1.start();
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //------------------------------------------------MAIN MENU BUTTON CODES-------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- public function playGame(e:MouseEvent):void
- {
- mainMenu.visible = false;
- filmGrain.visible = true;
- initGame();
- }
- public function showCredits(e:MouseEvent):void
- {
- creditsMenu.visible = true;
- }
- public function goBack(e:MouseEvent):void
- {
- creditsMenu.visible = false;
- }
- public function continueTo(e:MouseEvent):void
- {
- currentLevel += 1;
- initGame();
- storyMenu.visible = false;
- filmGrain.visible = true;
- }
- public function toMainMenu(e:MouseEvent):void
- {
- endMenu.visible = false;
- mainMenu.visible = true;
- }
- public function playAgain(e:MouseEvent):void
- {
- initGame();
- gameOverMenu.visible = false;
- filmGrain.visible = true;
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //--------------------------------------------------GAME LOOP CODE-------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Game Loop
- private function gameLoop(evt:TimerEvent):void //the loop keeps the player going and updates the textboxes.
- {
- this.player.movePlayer(); //move player
- this.player.shootBullet()//
- showFullHealth();
- secretCode();
- bonus.text = bonusPoints.toString();
- pointsText.text = points.toString();
- }
- //Method: Bonus Timer
- public function time(evt:TimerEvent):void //this counts down the bonus points. (acts as a challenge for players to get the most points)
- {
- bonusPoints = 120 - playTimer.currentCount;
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //------------------------------------------------------MUSIC CODE-------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Music Timers
- public function musicTime1(evt:TimerEvent):void
- {
- suspense2.play();
- musicTimer2.start();
- musicTimer1.reset();
- }
- public function musicTime2(evt:TimerEvent):void
- {
- suspense1.play();
- musicTimer2.reset();
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //-------------------------------------------------------GET CODES-------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Get Platforms
- public function getPlatforms():Dictionary
- {
- return this.platforms;
- }
- //Method: Get Walls
- public function getWalls():Dictionary
- {
- return this.walls;
- }
- //Method: Get Enemies
- public function getEnemies():Dictionary
- {
- return this.enemies;
- }
- // Method: Move vertically
- public function moveCamVertical(iDy:Number)
- {
- this.level.y += iDy;
- this.yDiff += iDy;
- }
- //Method: Get yDiff
- public function getYDiff():Number
- {
- return this.yDiff;
- }
- //Method: Get Player
- public function getPlayer():MovieClip
- {
- return this.player;
- }
- //Method: Get hpDrops
- public function getHpDrops():Dictionary
- {
- return this.hpDrops;
- }
- //Method: Get endPoints
- public function getEndPoints():Dictionary
- {
- return this.endPoints;
- }
- //Method: Get LastLevel
- public function getLastLevel():Boolean
- {
- return this.lastLevel;
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //----------------------------------------------PLAYER HEALTH CODES------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: MinusHealth
- public function minusHealth()
- {
- playerHealth -= 1;
- }
- //Method: AddHealth
- public function addHealth()
- {
- playerHealth += 1;
- checkHealth();
- }
- //Method: CheckHealth
- public function checkHealth() //HealthBars show according to the player's health
- {
- if (playerHealth == 5)
- {
- hB5.visible = true;
- hB4.visible = true;
- hB3.visible = true;
- hB2.visible = true;
- hB1.visible = true;
- }
- else if (playerHealth == 4)
- {
- hB5.visible = false;
- hB4.visible = true;
- hB3.visible = true;
- hB2.visible = true;
- hB1.visible = true;
- }
- else if (playerHealth == 3)
- {
- hB4.visible = false;
- hB3.visible = true;
- hB2.visible = true;
- hB1.visible = true;
- }
- else if (playerHealth == 2)
- {
- hB3.visible = false;
- hB2.visible = true;
- hB1.visible = true;
- }
- else if (playerHealth == 1)
- {
- hB2.visible = false;
- hB1.visible = true;
- }
- else if (playerHealth == 0)
- {
- hB1.visible = false;
- gameOver();
- }
- else if (playerHealth > 5)
- {
- playerHealth = 5;
- }
- }
- //Method: Show fullHealth
- public function showFullHealth()
- {
- if(playerHealth == 5)
- {
- fullHealth.visible = true;
- }
- else
- {
- fullHealth.visible = false;
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //----------------------------------------------------POINTS CODES-------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- public function addPoints()
- {
- points += 100;
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //-------------------------------------------------END LEVEL & GAME CODES------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- public function levelEnd() //ends the level
- {
- points = points + bonusPoints;
- //reset all the timers.
- gameTimer.stop();
- gameTimer.reset();
- playTimer.stop();
- playTimer.reset();
- //remove all children from the stage
- if(this.contains(player))
- {
- removeChild(player);
- }
- if(this.contains(level))
- {
- removeChild(level);
- }
- if(!lastLevel)
- {
- //levelUp();
- player.visible = false;
- level.visible = false;
- storyMenu.visible = true;
- filmGrain.visible = false;
- }
- else if (lastLevel)
- {
- gameEnd();
- }
- }
- public function gameEnd()
- {
- currentLevel = 1;
- clearDict();
- filmGrain.visible = false;
- endMenu.visible = true;
- endMenu.finalPoints.text = points;
- }
- public function gameOver()
- {
- gameTimer.stop();
- gameTimer.reset();
- playTimer.stop();
- playTimer.reset();
- if(this.contains(player))
- {
- removeChild(player);
- }
- if(this.contains(level))
- {
- removeChild(level);
- }
- gameOverMenu.visible = true;
- filmGrain.visible = false;
- gameOverMenu.deathAnimation.gotoAndPlay(1);
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //-----------------------------------------------CLEAR DICTOINARIES CODES------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- public function clearDict():void //Clears all items from all dictionaries
- {
- for (var hp in this.hpDrops)
- {
- delete hpDrops[hp];
- }
- for (var en in this.enemies)
- {
- delete enemies[en];
- }
- for (var wall in this.walls)
- {
- delete walls[wall];
- }
- for (var pf in this.platforms)
- {
- delete platforms[pf];
- }
- for (var ep in this.endPoints)
- {
- delete endPoints[ep];
- }
- }
- //The secret Code
- public function secretCode()
- {
- if (secretHere)
- {
- if (this.player.hitTestObject(level.secret))
- {
- secretFound.visible = true;
- if(hitSecret)
- {
- points += 1000;
- hitSecret = false;
- }
- }
- }
- else
- {
- secretFound.visible = false;
- }
- }
- }
- }
- //-------------------------------------------------------------------------------------------------
- //Program: PSP_Assignment2_Jon(s10105455k)_Shafi(s10087528k)
- //Created by: Jon Michael Jubelag & Adam Adil Shafi
- //Date Created: Week 13
- //Date Modified: 2nd Feb 2012
- //Description: The Assignment 2
- //------------------------------------------------------------------------------------------------
- package
- {
- import flash.events.*;
- import flash.display.*;
- import flash.ui.Keyboard;
- import flash.utils.Dictionary;
- import flash.utils.Timer;
- import fl.transitions.Tween;
- import fl.transitions.easing.*;
- import fl.transitions.TweenEvent;
- import flash.media.Sound;
- import flash.media.SoundChannel;
- public class Player extends MovieClip
- {
- //Properties
- private var main:MovieClip;//Store the main timeline
- private var dx:Number;
- private var dy:Number;
- private var jump:Boolean;
- private var jumping:Boolean;
- private var falling:Boolean;
- private var grounded:Boolean;
- private var moveLeft:Boolean;
- private var moveRight:Boolean;
- private var platforms:Dictionary;
- private var walls:Dictionary;
- private var enemies:Dictionary;
- private var hpDrops:Dictionary;
- private var endPoints:Dictionary;
- private var yDiff:Number;
- private var clingTimer:Timer;
- private var clingLeft:Boolean;
- private var clingRight:Boolean;
- private var jumpTimer:Timer;
- private var inAirUp:Boolean;
- private var inAirDown:Boolean;
- private var bullet:MovieClip; //To store the Bullet
- private var shoot:Boolean; //To shoot bullet
- private var dir:int; // direction for bullet
- private var hitBack:Boolean;
- private var enemyDetect:Boolean;
- private var enemyAlive:Boolean;
- private var hurt:Boolean;
- private var vulnerable:Boolean;
- private var invulTimer:Timer;
- private var playerHealth:Number;
- private var gun357:Sound;
- private var gun357Channel:SoundChannel;
- private var beep:Sound;
- private var beepChannel:SoundChannel;
- private var shootTrue:Boolean;
- private var shootTimer:Timer;
- public function Player(iMain:MovieClip)
- {
- //Constructor
- this.dx = 10;
- this.dy = 5;
- this.main = iMain;//pass main timeline into player class
- this.enemies = this.main.getEnemies();
- this.walls = this.main.getWalls();
- this.platforms = this.main.getPlatforms();//takes the private platform variable from GameControl.as and stores it into the platform variable in Player.as
- this.hpDrops = this.main.getHpDrops();
- this.endPoints = this.main.getEndPoints();
- this.main.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
- this.main.stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
- this.main.stage.addEventListener(KeyboardEvent.KEY_DOWN, jumpKeyHandler);
- this.main.stage.addEventListener(KeyboardEvent.KEY_DOWN, shootKeyHandler);
- this.main.stage.addEventListener(TweenEvent.MOTION_FINISH, tweenFinish);
- gun357 = new gun1();
- gun357Channel = new SoundChannel();
- beep = new beep1();
- beepChannel = new SoundChannel();
- clingTimer = new Timer(500,1);
- clingTimer.addEventListener(TimerEvent.TIMER_COMPLETE, clingDone);
- jumpTimer = new Timer(450, 1);
- jumpTimer.addEventListener(TimerEvent.TIMER_COMPLETE, jumpGap);
- this.dir = 1;
- bullet = new Bullet(this); //Create a copy of bullet
- enemyDetect = false;
- this.addEventListener(Event.ENTER_FRAME, detectPlayer);
- vulnerable = true;
- hurt = false;
- this.addEventListener(Event.ENTER_FRAME, playerHurt);
- invulTimer = new Timer (1500, 1);
- invulTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
- this.addEventListener(Event.ENTER_FRAME, getHp);
- this.addEventListener(Event.ENTER_FRAME, endLevel);
- playerHealth = 5;
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //------------------------------------------------KEY HANDLER CODES------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Key down handler;
- private function keyDownHandler(evt:KeyboardEvent):void
- {
- if (evt.keyCode == Keyboard.LEFT)
- {
- this.moveLeft = true;
- this.moveRight = false;
- this.scaleX = -1;
- this.dir = -1;
- }
- else if (evt.keyCode == Keyboard.RIGHT)
- {
- this.moveRight = true;
- this.moveLeft = false;
- this.scaleX = 1;
- this.dir = 1;
- }
- }
- //Method: Jump key Handler
- private function jumpKeyHandler(evt:KeyboardEvent):void
- {
- if (evt.keyCode == Keyboard.SPACE)
- {
- this.jump = true;
- this.main.stage.removeEventListener(KeyboardEvent.KEY_DOWN, jumpKeyHandler);
- jumpTimer.start();
- }
- }
- //Method: Shoot Key Handler
- public function shootKeyHandler(evt:KeyboardEvent):void
- {
- if (evt.keyCode == 88)
- {
- this.shoot = true;
- }
- }
- //Method: Key up handler
- private function keyUpHandler(evt:KeyboardEvent):void
- {
- if (evt.keyCode == Keyboard.LEFT)
- {
- this.moveLeft = false;
- }
- else if (evt.keyCode == Keyboard.RIGHT)
- {
- this.moveRight = false;
- }
- else if (evt.keyCode == Keyboard.SPACE)
- {
- this.jump = false;
- }
- else if (evt.keyCode == 88)
- {
- this.shoot = false;
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //------------------------------------------------JUMP/CLING TIMER CODES-------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Jump Gap
- public function jumpGap(evt:TimerEvent):void //ensure that the player cannot hold down the jump button.
- {
- this.main.stage.addEventListener(KeyboardEvent.KEY_DOWN, jumpKeyHandler);
- jumpTimer.reset();
- }
- //Method: Tween Finish
- public function tweenFinish(evt:Event):void
- {
- this.dy = 5;
- this.dx = 10;
- }
- //Method: Cling Timer
- public function clingDone(evt:TimerEvent):void //Pushes the player away from wall after timer is done.
- {
- if (clingRight)
- {
- this.x += 15;
- clingRight = false;
- this.dy = 5;
- this.dx = 10;
- }
- if (clingLeft)
- {
- this.x -= 15;
- clingLeft = false;
- this.dy = 5;
- this.dx = 10;
- }
- clingTimer.reset();
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //------------------------------------------------BULLET CODE------------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Shoot Bullet
- public function shootBullet():void
- {
- if (shoot && !clingLeft && !clingRight)//player cannot shoot while clinging
- {
- if(!bullet.getIsMoving()) //Check if bullet is fired
- {
- gun357.play();
- hitBack = true;
- //Spawn the bullet
- bullet.x = this.x + (50 * dir);
- bullet.y = this.y - this.height/2;
- this.main.addChild(bullet);
- this.addEventListener(Event.ENTER_FRAME, bulletHit);
- bullet.shoot(dir);
- }
- if(!moveLeft && !moveRight && !hurt)
- {
- this.gotoAndPlay(5);
- }
- if(moveLeft || moveRight && !hurt)
- {
- this.gotoAndPlay(7);
- }
- }
- }
- // Method: Remove Bullet
- public function removeBullet():void
- {
- this.main.removeChild(bullet);
- }
- //Method: Bullet Hit
- public function bulletHit(e:Event):void //Makes sure bullets cant go through walls
- {
- for (var wall in this.walls)
- {
- if (this.bullet.hitTestObject(wall.hitBoxWallLeft) || this.bullet.hitTestObject(wall.hitBoxWallRight))
- {
- bullet.y = 1000;
- }
- }
- for (var pf in this.platforms) //Makes sure bullets cant go through platforms
- {
- if (this.bullet.hitTestObject(pf.hitBoxPfLeftSide) || this.bullet.hitTestObject(pf.hitBoxPfRightSide))
- {
- bullet.y = 1000;
- }
- }
- for (var en in this.enemies)
- {
- if (this.bullet.hitTestObject(en.enemyBody))
- {
- en.minusEnemyHealth()
- if(en.getHealth() == 0)
- {
- en.gotoAndPlay(7);
- delete enemies[en];
- }
- bullet.y = 1000;
- }
- if (this.bullet.hitTestObject(en.enemyBack) && hitBack) //checks if bullet hits the back of enemy.
- {
- en.enemyBackHit();
- hitBack = false;
- }
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //------------------------------------------------MOVEMENT CODE----------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Move Player
- public function movePlayer():void
- {
- {
- this.y += this.dy;//move vertically
- if (! jumping && ! falling)
- {
- this.dy = 5;
- this.inAirDown = false;
- this.falling = true;
- }
- if (moveLeft) //equivalent to if(moveLeft==true)
- {
- this.x -= this.dx;
- if (moveLeft && this.x <= 400)
- {
- this.dx = 0;
- main.getChildByName("level").x += 10;
- }
- else
- {
- this.dx = 10;
- }
- if(!shoot)
- {
- this.gotoAndPlay(1);
- }
- }
- if (moveRight)
- {
- this.x += this.dx;
- if (moveRight && this.x >= 500)
- {
- this.dx = 0;
- main.getChildByName("level").x -= 10;
- }
- else
- {
- this.dx = 10;
- }
- if(!shoot)
- {
- this.gotoAndPlay(1);
- }
- }
- if (!moveRight && !moveLeft && !jump && !shoot && !jumping && !hurt)// && !clingLeft && !clingRight && grounded)
- {
- this.gotoAndPlay(3);
- }
- if (hurt)
- {
- this.gotoAndPlay(15);
- }
- if (!jumping && inAirDown && !grounded)
- {
- this.gotoAndPlay(11);
- }
- if (jump && grounded)
- {
- this.dy -= 50;//Jump power
- jump = false
- this.jumping = true;
- this.grounded = false;
- this.falling = false;
- inAirDown = false;
- }
- if (jump && clingLeft)
- {
- clingLeft = false;
- jump = false;
- this.dy = 5;
- this.dx = 10;
- new Tween (this, 'x', Regular.easeOut, this.x, this.x -= 55, 8, false);
- new Tween (this, 'y', Regular.easeOut, this.y, this.y - 110, 8, false);
- clingTimer.reset();
- }
- if (jump && clingRight)
- {
- clingRight = false;
- jump = false
- this.dy = 5;
- this.dx = 10;
- new Tween (this, 'x', Regular.easeOut, this.x, this.x + 55, 8, false);
- new Tween (this, 'y', Regular.easeOut, this.y, this.y - 110, 8, false);
- clingTimer.reset();
- }
- if (jumping)
- {
- if(!inAirDown)
- {
- this.gotoAndPlay(9);
- }
- this.dy *= 0.8;//Slow it down
- if (this.dy > -1)
- {
- this.dy = 5;
- falling = true;
- jumping = false;
- this.inAirDown = true;
- }
- }
- if (falling)
- {
- this.dy *= 1.18;
- grounded = false;
- if (dy > 35)
- {
- dy = 35;
- }
- }
- //--------------//
- // Platforms //
- //--------------//
- //to check if it his the platform
- // pf is the object key to point to each item in the dictionary.
- for (var pf in this.platforms)
- {
- if (this.hitBoxPlayer.hitTestObject(pf.hitBoxPfTop) && falling)
- {
- this.dy = 0;//set vertical speed to 0
- this.grounded = true;
- this.falling = false;
- this.inAirDown = false;
- this.y = pf.y + main.getYDiff();
- }
- if (this.hitTestObject(pf.hitBoxPfBottom))
- {
- this.y += 15; //pushes player away
- this.dy = 15;
- }
- if (this.hitTestObject(pf.hitBoxPfLeftSide))
- {
- this.x -= 8;
- }
- if (this.hitTestObject(pf.hitBoxPfRightSide))
- {
- this.x += 8;
- }
- }
- //-----------//
- // Walls //
- //-----------//
- for (var wall in this.walls)
- {
- if (this.playerBody.hitTestObject(wall.hitBoxWallLeft))
- {
- this.x = wall.x - 45 + main.getChildByName("level").x;
- if (jumping || falling)
- {
- clingLeft = true;
- }
- }
- if (this.playerBody.hitTestObject(wall.hitBoxWallRight))
- {
- this.x = wall.x + 45 + main.getChildByName("level").x;
- if (jumping || falling)
- {
- clingRight = true;
- }
- }
- }
- if (clingLeft)
- {
- clingTimer.start();
- this.dy = 0;
- this.dx = 1;
- this.scaleX = -1;
- this.gotoAndPlay(13);
- }
- if (clingRight)
- {
- clingTimer.start();
- this.dy = 0;
- this.dy = 1;
- this.scaleX = 1;
- this.gotoAndPlay(13);
- }
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //------------------------------------------------DETECTION CODE---------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Detect Player
- public function detectPlayer(e:Event):void
- {
- for (var en in this.enemies)
- {
- if (this.hitTestObject(en.enemySight))
- {
- enemyDetect = true;
- en.playerDetected();
- }
- else
- {
- en.playerUnDetected();
- }
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //------------------------------------------------PLAYER HURT CODE-------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Player Hurt
- public function playerHurt(e:Event):void
- {
- for (var en in this.enemies)
- {
- if (this.playerBody.hitTestObject(en.enemyBody) && vulnerable)
- {
- playerHealth -= 1;
- vulnerable = false;
- invulTimer.start();
- main.minusHealth();
- main.checkHealth();
- hurt = true;
- }
- else
- {
- hurt = false; //hurt is used to control certain animations
- }
- }
- }
- //Method: invul Timer Done
- public function timerDone(evt:TimerEvent):void //the invul timer prevents players from getting hurt too fast.
- {
- vulnerable = true;
- invulTimer.reset();
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //------------------------------------------------GET HP DROP CODE-------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: get HP
- public function getHp(evt:Event):void
- {
- for (var hp in this.hpDrops)
- {
- if (this.hitTestObject(hp) && (this.playerHealth < 5))
- {
- beep.play();
- playerHealth += 1;
- main.addHealth();
- hp.visible = false;
- delete hpDrops[hp];
- break;
- }
- }
- }
- public function endLevel(evt:Event):void
- {
- for (var eP in this.endPoints)
- {
- if (this.hitTestObject(eP))
- {
- main.levelEnd();
- }
- }
- }
- }
- }
- //-------------------------------------------------------------------------------------------------
- //Program: PSP_Assignment2_Jon(s10105455k)_Shafi(s10087528k)
- //Created by: Jon Michael Jubelag & Adam Adil Shafi
- //Date Created: Week 13
- //Date Modified: 2nd Feb 2012
- //Description: The Assignment 2
- //------------------------------------------------------------------------------------------------
- package
- {
- import flash.utils.Timer;
- import flash.display.*;
- import flash.events.*;
- import flash.utils.Dictionary;
- import flash.media.Sound;
- import flash.media.SoundChannel;
- public class Enemy extends MovieClip
- {
- //Properties
- private var main:MovieClip;
- private var player:MovieClip;
- private var yDiff:Number;
- private var dir:int; //Direction in which the enemy moves.
- private var dirChange:Boolean;
- private var dx:Number; //horizontal speed
- private var dy:Number; //vertical speed
- private var platforms:Dictionary;
- private var walls:Dictionary;
- private var enemyHp:Number;
- private var enemyAlive:Boolean;
- private var enemyHitSound:Sound;
- private var enemyDieSound:Sound;
- private var enemyHitSoundChannel:SoundChannel;
- private var enemyDieSoundChannel:SoundChannel;
- public function Enemy()
- {
- // constructor code
- this.dir = 1;
- this.dx = 5;
- this.dy = 10;
- this.addEventListener(Event.ENTER_FRAME, moveEnemy);
- this.addEventListener(Event.ENTER_FRAME, changeDir);
- this.addEventListener(Event.ENTER_FRAME, changeDirWall);
- dirChange = true;
- this.enemyHp = 5;
- enemyAlive = true;
- enemyHitSound = new eHit();
- enemyHitSoundChannel = new SoundChannel();
- enemyDieSound = new eDeath();
- enemyHitSoundChannel = new SoundChannel();
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //---------------------------------------------------SET MAIN CODE-------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Set Main
- public function toMain(iMain:MovieClip):void
- {
- this.main = iMain;
- this.platforms = this.main.getPlatforms();
- this.walls = this.main.getWalls();
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //------------------------------------------------PLAYER DETECTION CODE--------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Player Detected
- public function playerDetected() //enemy runs faster if player is in sight
- {
- this.dx = 15;
- dirChange = false;
- if(enemyAlive)
- {
- this.gotoAndPlay(3);
- }
- }
- public function playerUnDetected() //reverts back to normal speed
- {
- this.dx = 5;
- dirChange = true;
- if(enemyAlive)
- {
- this.gotoAndPlay(1);
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //-------------------------------------------------MOVE ENEMY CODE-------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Move Enemy
- private function moveEnemy(e:Event)
- {
- changeSightLength()
- this.y += this.dy;
- this.x += this.dx * dir;
- for (var pf in this.platforms)
- {
- if (this.enemyBody.hitTestObject(pf.hitBoxPfTop))
- {
- this.dy = 0;//set vertical speed to 0
- this.y = pf.y;
- }
- else
- {
- this.dy = 10;
- }
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //-------------------------------------------------DIRECTION CODES-------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Change Enemy Direction
- //Changes the direction in which the enemy faces and moves when it hits a wall or the end of a platform
- public function changeDir(e:Event)
- {
- for (var pf in this.platforms)
- {
- if (this.enemyBody.hitTestObject(pf.hitBoxEnd1) && dirChange)
- {
- dir = dir * -1;
- this.scaleX *= -1;
- this.enemySight.width = 360;
- this.x += pf.hitBoxEnd1.width;
- }
- else if (this.enemyBody.hitTestObject(pf.hitBoxEnd2) && dirChange)
- {
- dir = dir * -1;
- this.scaleX *= -1;
- this.enemySight.width = 360;
- this.x -= pf.hitBoxEnd1.width;
- }
- }
- }
- public function changeDirWall(e:Event)
- {
- for (var wall in this.walls)
- {
- if (this.enemyBody.hitTestObject(wall.hitBoxWallLeft))
- {
- dir = dir * -1;
- this.scaleX *= -1;
- this.enemySight.width = 360;
- this.x -= wall.hitBoxWallLeft.width;
- }
- if (this.enemyBody.hitTestObject(wall.hitBoxWallRight))
- {
- dir = dir * -1;
- this.scaleX *= -1;
- this.enemySight.width = 360;
- this.x += wall.hitBoxWallLeft.width;
- }
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //------------------------------------------------ENEMY SIGHT CODE-------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Change Enemy Sight Length
- //Reduces the size of the enemy sights so it wont see through the walls.
- public function changeSightLength()
- {
- for (var wall in this.walls)
- {
- if (this.enemySight.hitTestObject(wall.hitBoxWallRight))
- {
- this.enemySight.width = this.x - wall.x;
- }
- else if (this.enemySight.hitTestObject(wall.hitBoxWallLeft))
- {
- this.enemySight.width = wall.x - this.x;
- }
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //------------------------------------------------ENEMY HEALTH CODES-----------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Minus Enemy Health
- public function minusEnemyHealth()
- {
- enemyHp -= 1;
- if (enemyHp > 0)
- {
- this.gotoAndPlay(5);
- enemyHitSound.play();
- }
- else if (enemyHp == 0)
- {
- enemyAlive = false;
- this.removeEventListener(Event.ENTER_FRAME, moveEnemy);
- this.main.addPoints();
- enemyDieSound.play();
- }
- }
- public function getHealth():Number
- {
- return this.enemyHp;
- }
- //Method: Change Direction when hit
- public function enemyBackHit() //flips the enemy if its hit in the back
- {
- if(enemyAlive)
- {
- dirChange = false;
- dir = dir * -1;
- this.scaleX *= -1;
- this.enemySight.width = 360;
- this.gotoAndPlay(5);
- }
- }
- public function getEnemyAlive():Boolean
- {
- return this.enemyAlive;
- }
- }
- }
- //-------------------------------------------------------------------------------------------------
- //Program: PSP_Assignment2_Jon(s10105455k)_Shafi(s10087528k)
- //Created by: Jon Michael Jubelag & Adam Adil Shafi
- //Date Created: Week 13
- //Date Modified: 2nd Feb 2012
- //Description: The Assignment 2
- //------------------------------------------------------------------------------------------------
- package
- {
- import flash.utils.Timer;
- import flash.events.*;
- import flash.display.*;
- import flash.utils.Dictionary;
- public class Bullet extends MovieClip
- {
- //Properties
- private var main:MovieClip;
- private var dir:int; //-1 means left, 1 means right
- private var speed:Number; //speed
- private var accel:Number; //acceleration
- private var moveTimer:Timer;
- private var lagTimer:Timer
- private var isMoving:Boolean;
- private var player:MovieClip;
- public function Bullet(iParent:MovieClip)
- {
- // constructor code
- this.dir = 1;
- this.speed = 20;
- this.accel = 1.2;
- this.isMoving = false
- player = iParent;
- moveTimer = new Timer (20, 0);
- moveTimer.addEventListener(TimerEvent.TIMER, moveTimerHandler);
- lagTimer = new Timer (700,1); //1 means run once only
- lagTimer.addEventListener(TimerEvent.TIMER_COMPLETE, lagTimerCompleteHandler);
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //------------------------------------------------TIMER HANDLER CODES----------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: moveTimer Handler
- private function moveTimerHandler(evt:TimerEvent):void
- {
- this.x += this.speed * accel * dir;
- }
- //Method: lagTimer Complete Handler
- private function lagTimerCompleteHandler(evt:TimerEvent):void
- {
- isMoving = false;
- lagTimer.reset();
- moveTimer.stop();
- player.removeBullet();
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //-----------------------------------------------SHOOT BULLET CODE-------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Shoot Bullet
- public function shoot(iDir:int):void
- {
- if (!isMoving)
- {
- dir = iDir;
- isMoving = true;
- moveTimer.start();
- lagTimer.start();
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------//
- //--------------------------------------------------IS MOVING CODE-------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------------------------------//
- //Method: Get isMoving properties
- public function getIsMoving():Boolean
- {
- return this.isMoving;
- }
- }
- }
Add Comment
Please, Sign In to add comment